[英]Trim data in multiple data frames using R
這是我正在使用的數據結構:
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
...
我在“ total_stats”中有54個這些數據幀,它們稱為slice1-54,每個包含約700行-每行對應一個單元格
我想基於列中的值排除單元格(行),然后將未被排除的單元格(行)放入另一個稱為“ trimmed_stats”的對象中。
例如,我要排除以下單元格:
totalGFP < 2000
totalRFP < 9000
Area < 300
我想將剩下的所有單元格(行)(總GFP大於2000,總RFP大於9000,面積大於50的單元格)放入另一個稱為“ trimmed_stats”的對象,該對象保持“ total_stats”的相同結構(當然排除不感興趣的單元格)。
我知道這是可能的,但是我很難把心思包裹在plyr包和應用函數上(學習過程很慢,但是我認為隨着我得到更多的例子,它將變得更容易修改)。
感謝您提供的所有幫助!
希望您能提供一個可重現的小示例,但這應該有所幫助:
# 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 )
由於在OP中提到了plyr
,所以我們開始:
library(plyr)
trimmed_stats <- llply(.data = total_stats, subset,
!totalGFP < 2000 & !totalRFP < 9000 & !Area < 300)
llply
需要l
IST作為輸入,並給出了結果作為l
IST。 並關注@ SimonO101的例子:如果預期的結果,而為d
ATA框架,改變llply
到ldply
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.