简体   繁体   中英

Apply a function to multiple dataframes

I have many dataframes where missing values are denoted by the character string 'NA' which are not understood as missing by R.

The lengthy solution would be to apply the following function to each dataframe:

mydf[mydf == 'NA'] <- NA

I want to apply the above function to many dataframes.

Consider the following example:

set.seed(123)
A=as.data.frame(matrix(sample(c('NA',1:10),10*10,T),10)))
B=as.data.frame(matrix(sample(c('NA',LETTERS[1:10]),10*10,T),10))
C=as.data.frame(matrix(sample(c('NA',letters[1:10]),10*10,T),10))

And my best try (which does not work):

target <- list(A, B, C)
lapply(target, function(x) x[x == 'NA'] <- NA )

You almost got it right. You just forgot R returns the last accessed element of a function. In your case, it was only a subset of each data frame, so set your function to return x and it works:

set.seed(123)
A = as.data.frame(matrix(sample(c('NA',1:10),10*10,T),10))
B = as.data.frame(matrix(sample(c('NA',LETTERS[1:10]),10*10,T),10))
C = as.data.frame(matrix(sample(c('NA',letters[1:10]),10*10,T),10))

target = list(A, B, C)
lapply(target, function(x) {
  x[x == 'NA'] <- NA
  return(x)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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