如何使用dplyr根据另一列中的部分字符值更新列值?

rda*_*tor 0 r dplyr

我有一个包含两列的大型数据框.我想根据左栏中的部分字符值更新右栏.

这是一个例子:

df <- structure(list(content = c("my new info", "information2", 
"information3", "information4", "my new information2", "my new information3", 
"information5", "information6", "information7", "information8"
), content_new = c("no new info", "no new info", "no new info", 
"no new info", "no new info", "no new info", "no new info", "no new info", 
"no new info", "no new info")), .Names = c("content", "content_new"
), class = "data.frame", row.names = c(NA, 10L))

print(df)

               content content_new
1          my new info no new info
2         information2 no new info
3         information3 no new info
4         information4 no new info
5  my new information2 no new info
6  my new information3 no new info
7         information5 no new info
8         information6 no new info
9         information7 no new info
10        information8 no new info
Run Code Online (Sandbox Code Playgroud)

这是我需要的结果:

               content         content_new
1          my new info         no new info
2         information2         no new info
3         information3         no new info
4         information4         no new info
5  my new information2 my new information2
6  my new informatino3 my new informatino3
7         information5         no new info
8         information6         no new info
9         information7         no new info
10        information8         no new info
Run Code Online (Sandbox Code Playgroud)

我想要实现的规则是:如果内容包含"新信息",则将值放在content_new中.我试过这段代码:

library(dplyr)
newdf <- mutate(df, content_new = ifelse(grepl("new information",content,fixed==FALSE) == TRUE,content,content_new)) 
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Error in function (string)  : 
  comparison (1) is possible only for atomic and list types
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样,我怎么能解决这个问题?提前谢谢了!

Sve*_*ein 5

你必须使用fixed = FALSE而不是fixed == FALSE:

mutate(df, content_new = ifelse(grepl("new information", content, fixed = FALSE),
                                content, content_new))
               content         content_new
1          my new info         no new info
2         information2         no new info
3         information3         no new info
4         information4         no new info
5  my new information2 my new information2
6  my new informatino3         no new info
7         information5         no new info
8         information6         no new info
9         information7         no new info
10        information8         no new info
Run Code Online (Sandbox Code Playgroud)