#-------------------------------------------------- # Replace all instances of value x in a data frame # with value y #-------------------------------------------------- # The basic approach is to apply a replacement function # to each column of the data frame. There are a # couple of different variations on this theme. # Setup some data df <- data.frame(x=rnorm(25), y=runif(25, min=-2, max=2)) #-------------------------------------------------- # Version 1, write a function and lapply it. # The replace() function nearly does what we want. # Then lapply() repeats the modified replace() for # each column. lapply() returns a list, so we # convert it back to a data frame. #-------------------------------------------------- replace.neg <- function(z) {replace(z, z<0, NA)} df.na <- as.data.frame(lapply(df, replace.neg)) data.frame(df,df.na) #-------------------------------------------------- # Version 2, use an anonymous function # Harder to read, so its harder to debug, # but it leaves fewer objects in the workspace. #-------------------------------------------------- df.na2 <- as.data.frame(lapply(df, function(z){replace(z, z<0, NA)})) data.frame(df,df.na2) #-------------------------------------------------- # Version 3, replace using extractors, see ?replace # Just to illustrate how you write a function # using "[]", which is what replace() does. #-------------------------------------------------- replace.na <- function(z) { z[is.na(z)]<-0 z # the last expression evaluated within a function # is what gets returned } df.zero <- as.data.frame(lapply(df.na, replace.na)) data.frame(df.na, df.zero) #-------------------------------------------------- # Version 4, use an anonymous function again #-------------------------------------------------- df.zero2 <- as.data.frame(lapply(df.na, function(z){z[is.na(z)]<-0; z})) data.frame(df.na, df.zero2)