`[`和dplyr :: select in mutate会返回不同的结果

时间:2017-05-11 12:57:22

标签: r select dplyr

考虑以下数据集

require(dplyr)
df <- data_frame(
  "a" = c(NA, 1:2),
  "b" = c(1:2, NA),
  "c" = c(3:2, NA),
  "d" = 10:12
)

这可以按预期工作

df_new <- df %>% 
  mutate("e" = rowSums(df[c("a", "b")], na.rm = TRUE))

然而,这会返回错误

df_new <- df %>% 
  mutate("e" = rowSums(select(df, a:b), na.rm = TRUE))
Error in mutate_impl(.data, dots) : NA/NaN argument
In addition: Warning messages:
1: In c(NA, 1L, 2L):c(1L, 2L, NA) :
  numerical expression has 3 elements: only the first used
2: In c(NA, 1L, 2L):c(1L, 2L, NA) :
  numerical expression has 3 elements: only the first used

我不理解这种行为,因为rowSums(select(df, a:b), na.rm = TRUE)按预期工作,返回一个mutate应该简单追加的向量。有什么解释吗?

0 个答案:

没有答案