【发布时间】:2018-06-24 14:48:50
【问题描述】:
似乎使用dplyr::mutate() 创建列不允许向量回收。为什么?
例子:
require(dplyr)
df <- data_frame(id = rep(1:5, each = 42), name = rep(letters[1:7], each = 6, times = 5))
现在:
df %>% mutate (tp = c(1:42)) #results in
Error in mutate_impl(.data, dots) :
Column `tp` must be length 210 (the number of rows) or one, not 42
当然
df$tp <- c(1:42) #works
我的变异代码是错误的还是回收在mutate() 中根本不起作用?
如果有帮助,我将 dplyr 0.7.2 与 RStudio 1.0.153 (Mac) 一起使用
【问题讨论】:
-
错误消息清楚地表明您必须指定所有值或其中一个值。不回收。
-
很公平。非常感谢。我只是认为这可能是可能的,但我的代码是错误的。悲伤的时光。不回收。
-
如果您需要回收,则可以使用
transform作为解决方法。比较BOD %>% transform(a = 1:2)和BOD %>% mutate(a = 1:2)。 -
谢谢! @G.Grothendieck,变换是个好主意 :)