简体   繁体   English

如果数据框列中的变量名称与向量重命名变量中的名称匹配

[英]if variable name in data frame column matches names in a vector rename variable

Given a data frame and a vector给定一个数据框和一个向量

set.seed(123)

feature <- sample(LETTERS,30,replace = T)
number<-sample(1:100,30, replace = T)
df<-data.frame(feature,number)

rename<-c("N","V","C","E")

I want to scan through df$feature and if a letter stored in rename matches one in the column df$feature I want to rename them to "other" .我想扫描df$feature ,如果rename中存储的字母与df$feature列中的一个字母匹配,我想将它们重命名为"other"

I am quite sure that this must have been answered somewhere already, I have looked for a quite long time though.我很确定这一定已经在某个地方得到了回答,不过我已经找了很长时间了。

You can use %in% to find the rows which hold rename :您可以使用%in%查找包含rename的行:

df$feature[df$feature %in% rename] <- "other"

In case df$feature is a factor and does not contain other in the levels you need to add other to the levels before you exchange them with:如果df$feature是一个factor并且不包含other级别,则您需要在与以下级别交换之前将other添加到级别:

levels(df$feature) <- unique(c(levels(df$feature), "other"))

or you cast it to character with:或者您将其转换为character

df$feature <- as.character(df$feature)

One option using dplyr :使用dplyr一种选择:

df %>%
  mutate(feature = str_replace(feature, paste(rename, collapse = "|"), "other"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM