Apply a function to multiple dataframes

狂风中的少年 提交于 2021-01-28 12:32:08

问题


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 )

回答1:


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


来源:https://stackoverflow.com/questions/32371140/apply-a-function-to-multiple-dataframes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!