我有一个名为“confidence_table”的tibble . 有谁知道为什么如果我尝试使用mutate动词添加新列,这不起作用?
# A tibble: 12 x 3
# Groups: Age [2]
Age Condition Prop
<fctr> <fctr> <dbl>
0 old 0.73993056
1 old 0.75590278
0 old 0.15069444
1 old 0.13090278
0 new 0.06388889
1 new 0.04965278
0 new 0.05902778
1 new 0.05416667
0 lure 0.23055556
1 lure 0.23645833
0 lure 0.13819444
1 lure 0.12013889
我在base r中使用了这个函数,它确实有效
confidence_table$Confidence <- as.factor(rep(c("HC", "LC"), times = 3, each = 2))
# A tibble: 12 x 4
# Groups: Age [2]
Age Condition Prop Confidence
<fctr> <fctr> <dbl> <fctr>
0 old 0.73993056 HC
1 old 0.75590278 HC
0 old 0.15069444 LC
1 old 0.13090278 LC
0 new 0.06388889 HC
1 new 0.04965278 HC
0 new 0.05902778 LC
1 new 0.05416667 LC
0 lure 0.23055556 HC
1 lure 0.23645833 HC
0 lure 0.13819444 LC
1 lure 0.12013889 LC
这是与基本r代码一起使用的预期输出 . 但是,如果我使用:
confidence_table <- confidence_table %>%
mutate(Confidence = rep(c("HC", "LC"), times = 3, each = 2))
它说:mutate_impl(.data,dots)中的错误:列置信度必须是长度6(组大小)或一个,而不是12
这有什么问题?
1 回答
在这种情况下,错误消息实际上应该可以帮助您找出问题所在 . 请注意
2 x 3 x 2 = 12
.正如评论中指出的那样,解决这个问题的一种方法是首先考虑
ungroup
.你也可以在没有_1796671的情况下做到这一点:
另一种选择是将“条件”分组 - 可能是这样的:
样本数据: