[英]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 我在“ total_stats”中有54个这些数据帧,它们称为slice1-54,每个包含约700行-每行对应一个单元格
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". 我想基于列中的值排除单元格(行),然后将未被排除的单元格(行)放入另一个称为“ 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). 我想将剩下的所有单元格(行)(总GFP大于2000,总RFP大于9000,面积大于50的单元格)放入另一个称为“ trimmed_stats”的对象,该对象保持“ total_stats”的相同结构(当然排除不感兴趣的单元格)。
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). 我知道这是可能的,但是我很难把心思包裹在plyr包和应用函数上(学习过程很慢,但是我认为随着我得到更多的例子,它将变得更容易修改)。
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: 由于在OP中提到了
plyr
,所以我们开始:
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. llply
需要l
IST作为输入,并给出了结果作为l
IST。 And to follow @SimonO101's example: if the desired result rather is a d
ata frame, change llply
to ldply
. 并关注@ SimonO101的例子:如果预期的结果,而为
d
ATA框架,改变llply
到ldply
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.