简体   繁体   English

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

[英]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.

相关问题 使用 dplyr,我应该如何根据另一列的值创建一列重复字符的字符串? - Using dplyr, how should I create a column of strings repeating a character based on the value of another column? r、dplyr:如何使用 gsub 根据另一列中的值转换一列中的值 - r, dplyr: how to transform values in one column based on value in another column using gsub 如何使用 dplyr 创建基于另一个值的列,而不必写下每个值? - How do I create a column based on values of another using dplyr without having to write down every value? 使用dplyr根据另一列中的值添加新列 - adding a new column based upon values in another column using dplyr 如何使用dplyr根据另一列的不同值在新列中填充不同的值? - How to fill different values in a new column based on different values of another column using dplyr? 如何使用 dplyr 根据另一列中的值选择列? - How do I select column based on value in another column with dplyr? 使用dplyr case_when根据来自另一列的值更改NA值 - using dplyr case_when to alter NA values based on value from another column 使用 dplyr 根据 DataFrame 中的另一列更改行值 - Change row value based on another column in DataFrame using dplyr 使用dplyr有条件地将列中的值替换为另一列中的值 - Conditionally replace the values in columns to value in another column using dplyr 使用dplyr基于列值对R中的值求和 - Summing values in R based on column value with dplyr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM