Reputation: 3021
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.
Upvotes: 2
Views: 127
Reputation: 887691
One option is to load the datasets in a list
with mget
and use rowSums
to create a logical vector for keeping/removing the rows from each of the dataset
out <- lapply(mget(ldf), function(dat) dat[rowSums(is.na(dat)) <= 3,, drop = FALSE])
If the original objects needs to be updated, then use list2env
(not recommended though)
list2env(out, .GlobalEnv)
Upvotes: 2