我的每一行都有一个包含字符串的数据框:
day <- c("day", "daily")
name <- c("name")
我想使用此规则注释我的数据集
col_string, col_annotated
It is a rainy day, day
Daily exercise, day
My name is, name
Hello, NA
作为最终输出(基于前面的组的第二列):
rightAnimation
有可能成功吗?
答案 0 :(得分:1)
library(dplyr)
df %>%
mutate(col_annotated = case_when(grepl("day", .$col_string, T) ~ "day",
grepl("name", .$col_string, T) ~ "name"))
答案 1 :(得分:0)
d <- data.frame(col_string = c('It is a rainy day',
'Daily exercise',
'My name is',
'Hello'))
d$col_annotated <- ifelse(grepl('day', d$col_string, T) | grepl('daily', d$col_string, T), 'day',
ifelse(grepl('name', d$col_string, T), 'name', NA))
d
## col_string col_annotated
## 1 It is a rainy day day
## 2 Daily exercise day
## 3 My name is name
## 4 Hello <NA>