简体   繁体   English

将功能应用于多个数据框

[英]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. 我有许多数据框,其中缺失值由字符串'NA'表示,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. 您只是忘了R返回函数的最后访问元素。 In your case, it was only a subset of each data frame, so set your function to return x and it works: 在您的情况下,它只是每个数据帧的子集,因此将您的函数设置为返回x

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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