Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dataframe

r

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

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

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")

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). Thanks~

like image 811
Rock Avatar asked Sep 16 '25 02:09

Rock


1 Answers

You could just do

df[,var][df[,var] == 5] <- NA
like image 144
Ramnath Avatar answered Sep 17 '25 17:09

Ramnath