【发布时间】:2019-07-23 15:21:11
【问题描述】:
这是我的数据
as_tibble(data)
# A tibble: 40 x 4
Trt V1 V2 V3
<fct> <dbl> <dbl> <dbl>
1 d1 0.0105 0.00940 0.0174
2 d1 0.0199 0.00897 0.00279
3 d1 0.00836 0.0104 0.00816
4 d1 0.00960 0.0131 0.00404
5 d1 0.00527 0.0123 0.00863
6 d1 0.0136 0.0115 0.0130
7 d1 0.0216 0.00591 0.0106
8 d1 0.00558 0.00890 0.00964
9 d2 0.0193 0.0116 0.0199
10 d2 0.0172 0.0165 0.0582
# ... with 30 more rows
我想在哪里使用V* 和Trt 执行aov,然后执行函数f2 中给出的所有其他统计数据
f2 <- function(y, Trt){
dt1 <- aov(y ~ Trt) %>%
emmeans(specs = "Trt")
dt2 <- coef(pairs(dt1)) %>%
select(2:5)
d3 <- contrast(dt1, dt2, adjust = "Dunnett") %>%
summary %>%
pull(p.value)
return(d3)
}
当我针对 Trt 一次运行一列 V* 时,我得到了想要的结果
f2(data$V1, data$Trt)
[1] 5.450331e-01 5.936861e-01 2.302477e-02 7.882583e-15
f2(data$V2, data$Trt)
[1] 5.217088e-01 1.722111e-01 4.030167e-05 4.439782e-13
我想将f2应用于所有以V* 开头的列。这段代码出错了
map2_dfr(data %>% select_if(is.double), data$Trt, f2)
Error: Mapped vectors must have consistent lengths:
* `.x` has length 3
* `.y` has length 40
我不知道为什么map2_dfr 不能一次选择一列。有什么帮助吗?
【问题讨论】:
-
我认为您需要传递列名(引用的)而不是值,然后使用
paste或reformulate创建公式。如果我们需要不带引号传递,那么可能需要在转换为字符后在里面创建公式