I have data frames in my environment, and I have stored the names of the same in a character vector.
ldf <- names(which(unlist(eapply(.GlobalEnv,is.data.frame))))
when I print ldf, it has a structure similar to this:
>ldf
[1] "tdf1" "tdf2"
[3] "tdf3"
I want to remove rows that has many na's from each of these data frames. I have created a function as follows:
remna <- function(df) {
df$countofna <- apply(df[,c(1:length(df))], 1,function(x) sum(is.na(x)))
df <- df[df$countofna <=3, ]
df$countofna <- NULL
df <- df[1:nrow(df)-1, ]
}
How do i apply the remna function on each of the dataframes with names stored in ldf. Without using ldf, I tried using a for loop:
for (i in names(which(unlist(eapply(.GlobalEnv,is.data.frame))))) {
remna(i)
}
however this fails at executing remna with the following error:
Error in df[, c(1:length(df))] : incorrect number of dimensions
The function remna works perfectly on individual data frames.