小编leo*_*uyi的帖子

dplyr mutate rowSums计算或自定义函数

我试图从行计算中改变一个新变量,rowSums如下所示

iris %>% 
  mutate_(sumVar = 
            iris %>% 
            select(Sepal.Length:Petal.Width) %>%
            rowSums)
Run Code Online (Sandbox Code Playgroud)

结果是"sumVar"被截断为其第一个值(10.2):

Source: local data frame [150 x 6]
Groups: <by row>

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species sumVar
1           5.1         3.5          1.4         0.2  setosa   10.2
2           4.9         3.0          1.4         0.2  setosa   10.2
3           4.7         3.2          1.3         0.2  setosa   10.2
4           4.6         3.1          1.5         0.2  setosa   10.2
5           5.0         3.6          1.4         0.2  setosa   10.2
6           5.4         3.9          1.7         0.4  setosa   10.2
..
Warning message:
Truncating vector to length 1 
Run Code Online (Sandbox Code Playgroud)

它应该 …

r dplyr

54
推荐指数
4
解决办法
4万
查看次数

标签 统计

dplyr ×1

r ×1