R: Test condition on column of dataframe elements within list; return smaller list -


my goal take list of dataframes, see if specific column of data frames has max value of 0, , if so, remove data frame list.

right looping on names of list. given r, there must better way. feel need function applied through lapply() right. i've considered ddply() think maybe overkill. here have far:

# make df of first element mycolumn <- rep ("elementa",times=10) values <- seq(1,10) <- data.frame(mycolumn,values) # make df of second element mycolumn <- rep ("elementb",times=10) values <- rep(0,10) b <- data.frame(mycolumn,values)  # bind dataframes df <- rbind(a,b)  #now split dataframes based on element name mylist <- split(df,df$mycolumn)  # loop through element lists , check max of 0 in values (name in names(mylist)) { # loop through list     if (max(mylist[[name]]$values) == 0) { # check max 0       mylist <- mylist[[-names]] # if 0, remove element list   } # close if } # close loop  error in -names : invalid argument unary operator 

i've tested code outside loop, , seems work.
appreciated. thanks!

you can use this:

mylist <- mylist[sapply(mylist, function(d) max(d$values) != 0)] 

instead of for() loop. let pass dataframes 0 rows, warning.

to ensure empty dataframes removed, use this:

mylist <- mylist[sapply(mylist, function(d) if(nrow(d)==0) false else max(d$values)!=0)] 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -