简体   繁体   中英

Trim data in multiple data frames using R

Here is that data structure of what I am working with :

head(total_stats[[1]])



 cellID         X         Y Area   AvgGFP DeviationGFP   AvgRFP DeviationsRFP Slice totalGFP totalRFP
1      1  7.645614  92.10175  285 4.880702     4.795811 31.98246     12.402424     0     1391     9115
2      2 11.246544 225.18664  434 4.179724     4.792214 21.69816      7.471494     0     1814     9417
3      3 17.641860 346.75194  645 5.973643     6.199398 23.16279      9.691027     0     3853    14940
4      4  8.267218 441.30854  363 5.641873     6.714264 16.78788      5.220197     0     2048     6094
5      5  5.390845 480.99296  284 6.045775     8.907932 26.59507     10.562691     0     1717     7553
6      6  6.728365 529.86779  416 5.038462     5.083255 24.06971     10.818433     0     2096    10013

...

I have 54 of these data frames in "total_stats", they are called slice1-54 and contain ~700 rows each - each row corresponds to a cell

I want to exclude cells(rows) based on values in columns and then put the cells(rows) that are not excluded into another object called "trimmed_stats".

For example, I want to exclude the following cells :

totalGFP < 2000

totalRFP < 9000

Area < 300

All cells(rows) that are left over (those with totalGFP greater than 2000, totalRFP greater than 9000, and Area greater than 50) I want to put into another object called "trimmed_stats", which maintains the same structure of "total_stats" (excluding of course the cells that are not of interest).

I know this is possible but I am having a hard time wrapping my mind around the plyr package and apply functions (the learning process is slow, but i think as I get more examples it will become easier to tinker).

Thanks for any and all help!

I wish you'd supply a small reproducible example but this should help:

#   Create a small function to extract the rows you are interested in
f <- function(x) x[ ! x$totalGFP < 2000 & ! x$totalRFP < 9000 & ! x$Area < 300 , ]

#  Apply it to each data.frame in your list
trim <- lapply( total_stats , f )

#  Combine the results into one data.frame if desired...
trimmed_stats <- do.call( rbind , trim ) 

Since plyr was mentioned in OP, here we go:

library(plyr)
trimmed_stats <- llply(.data = total_stats, subset,
                       !totalGFP < 2000 & !totalRFP < 9000 & !Area < 300)

llply takes a l ist as input, and gives the result as a l ist. And to follow @SimonO101's example: if the desired result rather is a d ata frame, change llply to ldply .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM