dplyr mutate rowSums计算或自定义函数 [英] dplyr mutate rowSums calculations or custom functions

查看:699
本文介绍了dplyr mutate rowSums计算或自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从排序计算中变换一个新变量,
表示 rowSums 如下

I'm trying to mutate a new variable from sort of row calculation, say rowSums as below

iris %>% 
  mutate_(sumVar = 
            iris %>% 
            select(Sepal.Length:Petal.Width) %>%
            rowSums)

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

the result is that "sumVar" is truncated to its first value(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 

应该是 rowwise 应用?或者在这些计算中使用什么正确的动词。

Should it be rowwise applied? Or what's the right verb to use in these kind of calculations.

编辑:

更具体地说,有没有办法通过 dplyr 实现内联定制功能?

More specifically, is there any way to realize the inline custom function with dplyr?

我想知道如果可能的话,请执行以下操作:

I'm wondering if it is possible do something like:

iris %>% 
  mutate(sumVar = colsum_function(Sepal.Length:Petal.Width))


推荐答案

解决方法,但可以使用

iris %>% mutate(sumVar = rowSums(.[1:4]))

这篇关于dplyr mutate rowSums计算或自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆