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

标签 r string character data-cleaning grepl

我尝试根据另一列中是否存在特定字符值将“是/否”字符值添加到列中。

这是一个示例:

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

如果 example$V2.x 中第一列以 ^d^ll 开头,则为 example$ 中的值CLITIC 应该是 YES;如果没有,应该是NO。

因此,在上面的 df 中,example[1,2] 应该为 NO,example[2,2] 应该为 YES。

希望在数百行和十几列的数据集上自动执行此操作。不知道该怎么做,尽管 grepl() 似乎很有用。感谢您的帮助。

结构:

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")

最佳答案

您已经有了可在 grepl 中使用的正则表达式,它返回逻辑值。

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

要获取"is"/“否”值,请将其插入 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

关于r - 根据另一列中的初始字符串添加列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65699229/

相关文章:

r - 创建一个循环以在 ggplot 中写入注释

将小写字符串转换为大写?在 C 中

regex - perl6 正则表达式 : match all punctuations except . 和“

r - 如何使用 ggplot2 绘制非标准化数据?

r - 从 R 中的文件夹创建 zip 文件

string - 字符串 slice 是否执行基础数据的复制?

string - 对加密算法进行逆向工程

c++ - C++链表问题。数据类型之间有冲突?没有匹配的构造函数?

C++ 在数组或字符串中插入字符

Rstudio:Cmd + C/V 在编辑器中不起作用