[英]R tapply compressing results
I am using tapply to compress a large dataset on multiple species of plants grown under multiple conditions, in four separate experiments. 在四个独立的实验中,我正在使用Tapply压缩在多种条件下生长的多种植物上的大型数据集。 tapply then spits an output summarized by species.
然后轻按,然后输出按物种汇总的输出。 I am trying to then pull these separate species out of the output for further analyses.
然后,我试图将这些单独的物种从输出中拉出来,以进行进一步的分析。 So, tapply gives me this (from some dummy data) :
因此,tapply给我这个(来自一些虚拟数据):
> tapply(results, list(trial,condition,species),mean)
>
, , species1
A B C D
1 -0.6357911 0.6127278 -0.23812194 -0.2769131
2 -0.3851283 -0.5955274 -0.08072294 -0.7298832
3 0.2029780 -1.0282842 0.11518872 -0.6522809
4 -0.2254586 0.4215911 -0.84305584 -0.1108188
, , species2
A B C D
1 -0.4762501 0.35766102 -0.53821633 -0.64798979
2 0.0558234 0.18602479 -0.48208241 1.09532972
3 -1.0695515 0.84401536 -0.02232301 -0.02064807
4 -0.2423312 -0.02145042 -0.18834442 -0.08221573
Where I get the mean of each species, under each condition (AD), run in 4 separate experiments (1-4). 在每种条件下(AD),我得到每种物种的平均值的地方,在4个单独的实验中(1-4)进行。 Is it then possible, to, say, isolate species A, and then average 1-4 for each column?
那么,是否有可能分离出物种A,然后每列平均1-4个?
Not fully sure how your data is structured. 无法完全确定数据的结构。 but here's an attempt.
但是这是一个尝试。
#Data generation function runif
data_gen <- function() {
x <-
data.frame(A=runif(4, -1.005, 1.0049),
B=runif(4, -1.005, 1.0049),
C=runif(4, -1.005, 1.0049),
D=runif(4, -1.005, 1.0049))
return(x)
}
#Shape to similar structure
data <- list(species1=data_gen(),
species2=data_gen(),
species3=data_gen(),
species4=data_gen())
#1.lapply break it up to column 1 == A
#2.then sapply with a mean then transpose to 1 row
#3.and finally convert to data.frame
data_transf <- data.frame(t(sapply(lapply(data, '[[', 1),mean)))
#Rename columns if necessary
colnames(data_transf) <- paste0(colnames(data_transf),'_A_isolated_avg')
data_transf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.