[英]How to overwrite a variable in R based on a grep
I have a simple data frame: 我有一个简单的数据框:
> var_body_part <- c("eye and nose", "eye", "eye and ear", "eye and mouth", "foot", "foot", "ear", "ear", "foot", "mouth")
> var2 <- c("bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla")
> temp_df <- data.frame(var_body_part, var2)
So my data is: 所以我的数据是:
> temp_df
var_body_part var2
1 eye and nose bla
2 eye bla
3 eye and ear bla
4 eye and mouth bla
5 foot bla
6 foot bla
7 ear bla
8 ear bla
9 foot bla
10 mouth bla
Each time I find "eye" I want to replace the row with HEAD ie (see first 4 lines) 每次我找到“眼睛”我想用HEAD替换行,即(见前4行)
var_body_part var2
1 HEAD bla
2 HEAD bla
3 HEAD bla
4 HEAD bla
5 foot bla
6 foot bla
7 ear bla
8 ear bla
9 foot bla
10 mouth bla
It should be easy... I select the rows that are affected by the transformation with 它应该很简单......我选择受转换影响的行
temp_df$var_body_part[grep("eye", temp_df$var_body_part) ]
however I cannot find the correct statement to replace them with the correct value "HEAD". 但是我找不到正确的声明来用正确的值“HEAD”替换它们。
So far with my attempts I get a lot of 到目前为止,我的尝试得到了很多
invalid factor level, NA generated
Anybody can help? 有人可以帮忙吗?
The issue actually is that the columns got converted to factor
when creating the temp_df. 问题实际上是在创建temp_df时列被转换为
factor
。 Just use stringsAsFactors = FALSE
and you are good to go: 只需使用
stringsAsFactors = FALSE
就可以了:
temp_df <- data.frame(var_body_part, var2, stringsAsFactors = FALSE)
temp_df$var_body_part[grep("eye", temp_df$var_body_part)] <- "HEAD"
If you want to use factors, you can add "HEAD" to the levels of var_body_part
: 如果要使用因子,可以将“HEAD”添加到
var_body_part
的级别:
temp_df <- data.frame(var_body_part, var2, stringsAsFactors = TRUE)
levels(temp_df$var_body_part) <- c(levels(temp_df$var_body_part), "HEAD")
temp_df$var_body_part[grep("eye", temp_df$var_body_part)] <- "HEAD"
You can use transform
together with sub
: 你可以和
sub
一起使用transform
:
transform(temp_df, var_body_part = sub(".*eye.*", "HEAD", var_body_part))
The result: 结果:
var_body_part var2
1 HEAD bla
2 HEAD bla
3 HEAD bla
4 HEAD bla
5 foot bla
6 foot bla
7 ear bla
8 ear bla
9 foot bla
10 mouth bla
使用gsub()
非常简单:
mutate_at(temp_df, 'var_body_part', funs(gsub('.*eye.*', 'HEAD', .)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.