Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Replace values in selected columns by passing column name of data.frame into apply() or plyr function
- 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
- df
- a b c
- 1 1 4 NA
- 2 2 3 4
- 3 3 NA 3
- 4 4 2 2
- 5 5 1 1
- var <- c("b", "c")
- df <- within(df, sapply(var, function(x) x <- replace(x, x==5, NA)))
- 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
- df[,var][df[,var] == 5] <- NA
Add Comment
Please, Sign In to add comment