通过将data.frame的列名称传递给apply()或plyr函数来替换所选列中的值 [英] Replace values in selected columns by passing column name of data.frame into apply() or plyr function
问题描述
假设我有一个date.frame,如:
Suppose I have a date.frame like:
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
我需要将 5
中的所有替换为
b
& c
然后返回到 df
:
and I need to replace all the 5
as NA
in column b
& c
then return to 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
但是我想做一个通用的 apply()
函数,而不是使用 replace()
,因为实际上需要在实际数据中替换许多变量。假设我已经定义了一个变量列表:
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. Suppose I've defined a variable list:
var <- c("b", "c")
,并提出如下:
df <- within(df, sapply(var, function(x) x <- replace(x, x==5, NA)))
但没有任何反应。我正在想,如果有一种方法来处理与上述类似的事情,通过将列名称的变量列表从data.frame传递到通用的 apply / plyr
功能(或者其他一些完全不同的方式)。谢谢〜
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). 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
我发现ifelse符号在这里更容易理解,但大多数Rers可能会使用索引代替。
I find the ifelse notation easier to understand here, but most Rers would probably use indexing instead.
这篇关于通过将data.frame的列名称传递给apply()或plyr函数来替换所选列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!