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

查看:20
本文介绍了dplyr 改变 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))

推荐答案

您可以使用 rowwise() 函数:

You can use rowwise() function:

iris %>% 
  rowwise() %>% 
  mutate(sumVar = sum(c_across(Sepal.Length:Petal.Width)))

#> # A tibble: 150 x 6
#> # Rowwise: 
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species sumVar
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>    <dbl>
#>  1          5.1         3.5          1.4         0.2 setosa    10.2
#>  2          4.9         3            1.4         0.2 setosa     9.5
#>  3          4.7         3.2          1.3         0.2 setosa     9.4
#>  4          4.6         3.1          1.5         0.2 setosa     9.4
#>  5          5           3.6          1.4         0.2 setosa    10.2
#>  6          5.4         3.9          1.7         0.4 setosa    11.4
#>  7          4.6         3.4          1.4         0.3 setosa     9.7
#>  8          5           3.4          1.5         0.2 setosa    10.1
#>  9          4.4         2.9          1.4         0.2 setosa     8.9
#> 10          4.9         3.1          1.5         0.1 setosa     9.6
#> # ... with 140 more rows

c_across() 使用整洁的选择语法,因此您可以简洁地选择多个变量"'

"c_across() uses tidy selection syntax so you can to succinctly select many variables"'

最后,如果你愿意,你可以在最后使用 %>% ungroup 从 rowwise 退出.

Finally, if you want, you can use %>% ungroup at the end to exit from rowwise.

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

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