Search code examples
rfilterwhile-loopdimensions

Filtering out the data frame whose row dimension is zero in R


I have many data frames like df1,df2,...,df11 whose dimensions are 20,3; 40,4,; 0,5;...0,2; these dimensions does not remain constant in my program from where I'm calculating my data frames.So I want to filter out those data frames who have zero row dimensions like dim(df3) gives 0,5 This is what I have tried

while(!dim(df(i))[1]==0)
 {

  DF=paste("df",c(1:11))

 }

Thanks


Solution

  • We can use Filter after keeping the datasets in a list

    Filter(function(x) nrow(x)>1, mget(paste0("df", 1:11)))
    

    Or another option is sapply to create a logical vector and then subset the list

    lst <- mget(paste0("df", 1:11))
    i1 <- sapply(lst, nrow)>0 
    lst[i1]