检测字符串是否包含一个句点并在R中改变新列

问题描述 投票:1回答:2

我有这个df

structure(list(Name = c("Write arguments to support claims in an analysis of substantive topics or texts, using valid reasoning and relevant and sufficient evidence.", 
"Write opinion pieces on topics or texts, supporting a point of view with reasons and information.", 
"Organize information and ideas around a topic to plan and prepare to write.", 
"Introduce a topic or text clearly, state an opinion, and create an organizational structure in which ideas are logically grouped to support the writer's purpose.", 
"Provide logically ordered reasons that are supported by facts and details.", 
"Link opinion and reasons using words, phrases, and clauses.", 
"Provide a concluding statement or section related to the opinion presented.", 
"With guidance and support from peers and adults, develop and strengthen writing as needed by revising, editing, rewriting, or trying a new approach, with consideration to task, purpose, and audience.", 
"Write informative/explanatory texts to examine and convey complex ideas and information clearly and accurately through the effective selection, organization, and analysis of content.", 
"Write informative /explanatory texts to examine a topic and convey ideas and information clearly.", 
"Organize information and ideas around a topic to plan and prepare to write.", 
"Introduce a topic clearly, provide a general observation and focus, and group related information logically; include formatting, illustrations, and multimedia when useful to aiding comprehension.", 
"Develop the topic with facts, definitions, concrete details, quotations, or other information and examples related to the topic.", 
"Link ideas within and across categories of information using words, phrases, and clauses.", 
"Use precise language and domain-specific vocabulary to inform about or explain the topic.", 
"Provide a concluding statement or section related to the information or explanation presented.", 
"With guidance and support from peers and adults, develop and strengthen writing as needed by revising, editing, rewriting, or trying a new approach, with consideration to task, purpose, and audience."
), Identifier = c("W.51", "W.5.1", "W.5.1.a", "W.5.1.b", "W.5.1.c", 
"W.5.1.d", "W.5.1.e", "W.5.1.f", "W.52", "W.5.2", "W.5.2.a", 
"W.5.2.b", "W.5.2.c", "W.5.2.d", "W.5.2.e", "W.5.2.f", "W.5.2.g"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-17L), problems = structure(list(row = c(1359L, 1360L, 1381L, 
1382L), col = c("end_grade", "end_grade", "end_grade", "end_grade"
), expected = c("1/0/T/F/TRUE/FALSE", "1/0/T/F/TRUE/FALSE", "1/0/T/F/TRUE/FALSE", 
"1/0/T/F/TRUE/FALSE"), actual = c("12", "12", "12", "12"), file = c("'ela_dirty.csv'", 
"'ela_dirty.csv'", "'ela_dirty.csv'", "'ela_dirty.csv'")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame")), spec = structure(list(
    cols = list(Name = structure(list(), class = c("collector_character", 
    "collector")), Identifier = structure(list(), class = c("collector_character", 
    "collector")), ParentStandardIdentifier = structure(list(), class = c("collector_character", 
    "collector")), SubjectArea = structure(list(), class = c("collector_character", 
    "collector")), Description = structure(list(), class = c("collector_character", 
    "collector")), grade = structure(list(), class = c("collector_character", 
    "collector")), end_grade = structure(list(), class = c("collector_logical", 
    "collector")), learning_domain = structure(list(), class = c("collector_character", 
    "collector")), strand_title = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector"))), class = "col_spec"))

我想创建一个新列(test),其中包含来自Name列的值,该列的标识符只有一个句点。例如,对于行1-8,第一个Name值将被添加到test,第9行的Name值将被添加到第9-17行的test

我试图想出一个tidyverse解决方案无济于事。

这是我想要遵循的一般结构

df %>% 
   mutate(test = case_when(
        str_detect(Identifier, "{2}.") ~ 
r regex
2个回答
1
投票
df %>% mutate(test=ifelse(nchar(gsub("[^.]","",Identifier))>1,NA,Name)) %>% fill(test)
# A tibble: 17 x 3
#    Name                                     Identifier test                                   
#    <chr>                                    <chr>      <chr>                                  
#  1 Write arguments to support claims in an… W.51       Write arguments to support claims in a…
#  2 Write opinion pieces on topics or texts… W.5.1      Write arguments to support claims in a…
#  3 Organize information and ideas around a… W.5.1.a    Write arguments to support claims in a…
#  4 Introduce a topic or text clearly, stat… W.5.1.b    Write arguments to support claims in a…
#  5 Provide logically ordered reasons that … W.5.1.c    Write arguments to support claims in a…
#  6 Link opinion and reasons using words, p… W.5.1.d    Write arguments to support claims in a…
#  7 Provide a concluding statement or secti… W.5.1.e    Write arguments to support claims in a…
#  8 With guidance and support from peers an… W.5.1.f    Write arguments to support claims in a…
#  9 Write informative/explanatory texts to … W.52       Write informative/explanatory texts to…
# 10 Write informative /explanatory texts to… W.5.2      Write informative/explanatory texts to…
# 11 Organize information and ideas around a… W.5.2.a    Write informative/explanatory texts to…
# 12 Introduce a topic clearly, provide a ge… W.5.2.b    Write informative/explanatory texts to…
# 13 Develop the topic with facts, definitio… W.5.2.c    Write informative/explanatory texts to…
# 14 Link ideas within and across categories… W.5.2.d    Write informative/explanatory texts to…
# 15 Use precise language and domain-specifi… W.5.2.e    Write informative/explanatory texts to…
# 16 Provide a concluding statement or secti… W.5.2.f    Write informative/explanatory texts to…
# 17 With guidance and support from peers an… W.5.2.g    Write informative/explanatory texts to…

gsub("[^.]", "", Identifier)只留下点,nchar找到点数,fill填充缺失值。


0
投票

您可以使用str_count来识别值,然后使用fill来向前填充值

library(tidyverse)

df %>% 
  mutate(test = replace(Name, str_count(Identifier, '\\.') != 1,  NA)) %>% 
  fill(test)

# # A tibble: 17 x 3
#    Name                                      Identifier test                                     
#    <chr>                                     <chr>      <chr>                                    
#  1 Write arguments to support claims in an ~ W.51       Write arguments to support claims in an ~
#  2 Write opinion pieces on topics or texts,~ W.5.1      Write arguments to support claims in an ~
#  3 Organize information and ideas around a ~ W.5.1.a    Write arguments to support claims in an ~
#  4 Introduce a topic or text clearly, state~ W.5.1.b    Write arguments to support claims in an ~
#  5 Provide logically ordered reasons that a~ W.5.1.c    Write arguments to support claims in an ~
#  6 Link opinion and reasons using words, ph~ W.5.1.d    Write arguments to support claims in an ~
#  7 Provide a concluding statement or sectio~ W.5.1.e    Write arguments to support claims in an ~
#  8 With guidance and support from peers and~ W.5.1.f    Write arguments to support claims in an ~
#  9 Write informative/explanatory texts to e~ W.52       Write informative/explanatory texts to e~
# 10 Write informative /explanatory texts to ~ W.5.2      Write informative/explanatory texts to e~
# 11 Organize information and ideas around a ~ W.5.2.a    Write informative/explanatory texts to e~
# 12 Introduce a topic clearly, provide a gen~ W.5.2.b    Write informative/explanatory texts to e~
# 13 Develop the topic with facts, definition~ W.5.2.c    Write informative/explanatory texts to e~
# 14 Link ideas within and across categories ~ W.5.2.d    Write informative/explanatory texts to e~
# 15 Use precise language and domain-specific~ W.5.2.e    Write informative/explanatory texts to e~
# 16 Provide a concluding statement or sectio~ W.5.2.f    Write informative/explanatory texts to e~
# 17 With guidance and support from peers and~ W.5.2.g    Write informative/explanatory texts to e~
© www.soinside.com 2019 - 2024. All rights reserved.