我有一个包含两列的大型数据框。我想根据左栏中的部分字符值更新右栏。
这是一个例子:
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
这就是我需要的结果:
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
我想要实现的规则是:如果内容包含“新信息”,请将值放在content_new中。 我试过这段代码:
library(dplyr)
newdf <- mutate(df, content_new = ifelse(grepl("new information",content,fixed==FALSE) == TRUE,content,content_new))
我收到此错误:
Error in function (string) :
comparison (1) is possible only for atomic and list types
有谁知道为什么会这样,以及我如何解决这个问题?非常感谢提前!
答案 0 :(得分: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