我有一个包含三个分组变量的数据集:条件、子和延迟。这是我的数据的简化版本(真实数据要长得多)
sub condition delay later_value choiceRT later_choice primeRT cue 10 SIZE 10 27 1832 1 888 CHILD 10 PAST 5 11 298 0 1635 PANTS 10 SIZE 21 13 456 0 949 CANDY 11 SIZE 120 22 526 1 7963 BOY 11 FUTURE 120 27 561 1 4389 CHILDREN 11 PAST 5 13 561 1 2586 SPRING
我有一套复杂的程序来应用这些数据(细节并不重要)我编写了以下函数,当被三个分组变量分割时,它完成了我想要的。它返回 3 个我感兴趣的变量(indiff、p_intercept 和 p_lv)
getIndiffs <- function(currdelay){
if (mean(currdelay$later_choice) == 1) {
indiff = 10.5
p_intercept = "laters"
p_lv = "laters"
}
else if (mean(currdelay$later_choice) == 0) {
indiff = 30.5
# no p-val here, code that this was not calculated
p_intercept = "nows"
p_lv = "nows"
}
else {
F <- factor(currdelay$later_choice)
fit <- glm(F~later_value,data=currdelay,family=binomial())
indiff <- -coef(fit)[1]/coef(fit)[2]
if (indiff < 10) indiff = 10.5
else if (indiff > 30) indiff = 30.5
p_intercept = round(summary(fit)$coef[, "Pr(>|z|)"][1],3)
p_lv = round(summary(fit)$coef[, "Pr(>|z|)"][2], 3)
c(indiff,p_intercept,p_lv)
}
我正在尝试使用 ddply 将其应用于每个 3 个分组变量的数据子集:
ddply(数据,。(子,条件,延迟),getIndiffs)
但是,当我运行它时,我得到了错误
list_to_dataframe(res, attr(.data, "split_labels")) 中的错误:结果的长度不相等
奇怪的是,当我只使用 1 个分组变量但用 2+ 抛出错误时,这很好用
此外,当我自己“模拟”将数据集拆分为仅包含由 3 个分组变量拆分的子集的数据帧时,我的函数工作得很好。(注意:我尝试了返回 3 个变量甚至只返回 1 个变量的不同方法,但它也不起作用)
基本上,我想知道的是如何使用 plyr 来使用函数返回多个变量。
对我的问题的任何其他根本不同的解决方案也是受欢迎的。