问题
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