[英]how to renew column values based on part of character value in another column using dplyr?
I have a large data frame with two columns. 我有两列的大型数据框。 The right column I want to renew based on parts of the character values in the left column. 我想根据左列中部分字符值来续订右列。
This is anexample: 这是一个例子:
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
and this is the result I need: 这是我需要的结果:
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
The rule I want to implement is: if content includes "new information", put the value in content_new. 我要实现的规则是:如果内容包括“新信息”,则将值放在content_new中。 I tried this code: 我尝试了这段代码:
library(dplyr)
newdf <- mutate(df, content_new = ifelse(grepl("new information",content,fixed==FALSE) == TRUE,content,content_new))
I get this error: 我收到此错误:
Error in function (string) :
comparison (1) is possible only for atomic and list types
Does anyone know why this is happening and how I can solve this problem? 有谁知道为什么会这样以及我如何解决这个问题? Many thanks in advance! 提前谢谢了!
You have to use fixed = FALSE
instead of fixed == FALSE
: 您必须使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.