简体   繁体   English

通过将data.frame的列名传递给apply()或plyr函数来替换所选列中的值

[英]Replace values in selected columns by passing column name of data.frame into apply() or plyr function

Suppose I have a date.frame like: 假设我有一个date.frame,如:

df <- data.frame(a=1:5, b=sample(1:5, 5, replace=TRUE), c=5:1)
df
  a b c
1 1 4 5
2 2 3 4
3 3 5 3
4 4 2 2
5 5 1 1

and I need to replace all the 5 as NA in column b & c then return to df : 我需要在bc列中将所有5替换为NA然后返回到df

df
  a b  c
1 1 4  NA
2 2 3  4
3 3 NA 3
4 4 2  2
5 5 1  1

But I want to do a generic apply() function instead of using replace() each by each because there are actually many variables need to be replaced in the real data. 但我想做一个通用的apply()函数,而不是每个都使用replace() ,因为实际上有很多变量需要在真实数据中被替换。 Suppose I've defined a variable list: 假设我已经定义了一个变量列表:

var <- c("b", "c")

and come up with something like: 并想出类似的东西:

df <- within(df, sapply(var, function(x) x <- replace(x, x==5, NA)))

but nothing happens. 但没有任何反应。 I was thinking if there is a way to work this out with something similar to the above by passing a variable list of column names from a data.frame into a generic apply / plyr function (or maybe some other completely different ways). 我正在考虑是否有办法通过将data.frame中的列名变量列表传递给泛型apply / plyr函数(或者其他一些完全不同的方式)来解决与上述类似的问题。 Thanks~ 谢谢〜

df <- data.frame(a=1:5, b=sample(1:5, 5, replace=TRUE), c=5:1)
df
var <- c("b","c")
df[,var] <- sapply(df[,var],function(x) ifelse(x==5,NA,x))
df

I find the ifelse notation easier to understand here, but most Rers would probably use indexing instead. 我发现这里的ifelse符号更容易理解,但大多数Rers可能会使用索引。

你可以做到

df[,var][df[,var] == 5] <- NA

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

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