R在数据帧组上应用函数
我需要对每个受试者分别进行方差分析。 我有一个数据框架,包括来自37个受试者的数据,我不想循环37次,对每个受试者分别进行37次方差分析。 以下是我的数据的前4行:R在数据帧组上应用函数,r,split-apply-combine,R,Split Apply Combine,我需要对每个受试者分别进行方差分析。 我有一个数据框架,包括来自37个受试者的数据,我不想循环37次,对每个受试者分别进行37次方差分析。 以下是我的数据的前4行: latency subject trialcode 1 1385 14233664 neighbour 2 2493 14233664 neighbour 3 1429 14233664 neighbour 4 1884
latency subject trialcode
1 1385 14233664 neighbour
2 2493 14233664 neighbour
3 1429 14233664 neighbour
4 1884 14233664 neighbour
这是我的代码:
tmp <- with(as.data.frame(data),
by(data$subject,
function(x) aov(latency ~ trialcode + Error(trialcode), data=data)))
sapply(tmp, coef)
unique()仅适用于向量
谢谢你的帮助
谢谢我认为的电话是不正确的。如果我使用
数据(npk)
对于您的数据,它可能是
do.call(rbind, by(data[,-2], data$subject,
FUN=function(x) coef(aov(latency ~ trialcode+ Error(trialcode), x))))
或使用data.table
library(data.table)
setkey(setDT(data), subject)[, as.list(coef(aov(latency~trialcode+
Error(trialcode))),by=subject]
我不喜欢的
。请参阅以获取替代方案。
do.call(rbind, by(data[,-2], data$subject,
FUN=function(x) coef(aov(latency ~ trialcode+ Error(trialcode), x))))
library(data.table)
setkey(setDT(data), subject)[, as.list(coef(aov(latency~trialcode+
Error(trialcode))),by=subject]