简体   繁体   English

根据另一列中的初始字符串添加列值

[英]Adding column values based on initial character strings in another column

I'm trying to add "YES/NO" character values to a column based on wether there is a specific character value in another column.我正在尝试根据另一列中是否存在特定字符值将“是/否”字符值添加到列中。

Here is an example :这是一个example

     V2.x          Clitic
1    can could     NA
2    d should      NA

If the first column starts with ^d or ^ll in example$V2.x , the value in example$Clitic should be YES;如果example$V2.x中的第一列以^d^ll开头,则example$Clitic中的值应为 YES; if not, it should be NO.如果不是,它应该是NO。

So in the df above example[1,2] should be NO and example[2,2] should be YES.所以在上面的df中example[1,2]应该是NO, example[2,2]应该是YES。

Looking to automate this on a dataset of several hundred rows and a dozen columns.希望在数百行和十几列的数据集上自动执行此操作。 not sure how to do it, although grepl() seems useful.不知道该怎么做,虽然 grepl() 似乎很有用。 Would appreciate your help.感谢您的帮助。

Structure:结构:

structure(list(V2.x = structure(c(1L, 19L), .Label = c("can could", 
"can cud", "can may", "can might", "can should", "can will", 
"can would", "could can", "could may", "could might", "could should", 
"could used to", "could will", "d can", "d could", "d may", "d might", 
"d must", "d should", "d used to", "d will", "have to should", 
"have to will", "ll can", "ll could", "ll may", "ll might", "ll must", 
"ll shall", "ll should", "ll used to", "ll would", "may can", 
"may might", "may must", "may shall", "may should", "may used to", 
"may will", "may would", "might can", "might could", "might may", 
"might must", "might shall", "might should", "might will", "might would", 
"might wud", "must can", "must will", "must would", "shall can", 
"shall will", "should can", "should could", "should may", "should might", 
"should must", "should will", "should would", "used to could", 
"will can", "will could", "will kin", "will may", "will might", 
"will must", "will shall", "will should", "will would", "would can", 
"would could", "would may", "would might", "would must", "would should", 
"would will"), class = "factor"), Clitic = c(NA, NA)), row.names = 1:2, class = "data.frame")

You already have the regex to use in grepl which returns logical values.您已经拥有在grepl中使用的正则表达式,它返回逻辑值。

grepl('^(d|ll)', example$V2.x)
#[1] FALSE  TRUE

To get "Yes"/"No" values plug it in ifelse :要获得“是”/“否”值,请将其插入ifelse

example$Clitic <- ifelse(grepl('^(d|ll)', example$V2.x), 'Yes', 'No')
#Without ifelse
#example$Clitic <- c('No', 'Yes')[grepl('^(d|ll)', example$V2.x) + 1]
example

#       V2.x Clitic
#1 can could     No
#2  d should    Yes

We can use str_detect我们可以使用str_detect

library(stringr)
library(dplyr)
example %>%
   mutate(Clitic = case_when(str_detect(V2.x, "^(d|ll)") ~ "Yes", TRUE ~ "No"))    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM