[英]dplyr - inputting columns to rowwise() with column index instead of column name
[英]How to do rowwise summation over selected columns using column index with dplyr?
在dplyr
,如何对所选列执行行向求和(使用列索引)?
这不起作用
> iris %>% mutate(sum=sum(.[1:4])) %>% head
Sepal.Length Sepal.Width Petal.Length Petal.Width Species sum
1 5.1 3.5 1.4 0.2 setosa 2078.7
2 4.9 3.0 1.4 0.2 setosa 2078.7
3 4.7 3.2 1.3 0.2 setosa 2078.7
4 4.6 3.1 1.5 0.2 setosa 2078.7
5 5.0 3.6 1.4 0.2 setosa 2078.7
6 5.4 3.9 1.7 0.4 setosa 2078.7
我可以做到以下几点,但它并不美丽
> iris %>% mutate(index=1:n()) %>%
gather("param", "value", 1:4) %>%
group_by(index) %>%
mutate(sum=sum(value)) %>%
spread(param, value) %>% select(-index)
Source: local data frame [150 x 6]
Species sum Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 10.2 5.1 3.5 1.4 0.2
2 setosa 9.5 4.9 3.0 1.4 0.2
3 setosa 9.4 4.7 3.2 1.3 0.2
4 setosa 9.4 4.6 3.1 1.5 0.2
5 setosa 10.2 5.0 3.6 1.4 0.2
6 setosa 11.4 5.4 3.9 1.7 0.4
7 setosa 9.7 4.6 3.4 1.4 0.3
8 setosa 10.1 5.0 3.4 1.5 0.2
9 setosa 8.9 4.4 2.9 1.4 0.2
10 setosa 9.6 4.9 3.1 1.5 0.1
.. ... ... ... ... ... ...
是否有更多的语法更好的方法来实现这一目标?
编辑:它与其他问题不同,因为我想对使用列索引选择的列进行逐行操作“
正如评论中已经说过的那样,您可以通过以下方式完成任务:
iris %>% mutate(sum=Reduce("+",.[1:4]))
在这种情况下,基本rowSums
也可以工作:
iris$sum<-rowSums(iris[,1:4])
您可以(ab)使用基数R的subset
,它允许按数字选择列:
iris %>% subset(select=1:4) %>% mutate(sum=rowSums(.))
不确定这是正确的礼仪,但我更喜欢回收这个线程,而不是开始给出一个新的,我很确定,我只是做了一个菜鸟错误。
为什么这个工作正常:
test$sum <- Reduce("+", test[,3:ncol(test)])
而这(对我来说相同)不是吗?
test %>%
mutate(sum = Reduce("+",.[3:ncol(.)]))
给出的错误是
Error in mutate_impl(.data, dots) :
Column `sum` must be length 1 (the group size), not 915
从30'左右开始,我正在敲桌子!
我希望我可以给你底层数据集,但我真的不能。
第1列:2列是文本字段,而3:ncol(。)是TRUE / FALSE(逻辑)1。 ncol(。)= 33。
我认为执行逐行操作的能力是tidyverse语法的一个弱点,但是purrr:pmap_*
对于运行它很有用,尽管它并不那么明显:
iris %>%
mutate(total = pmap_dbl(select(., -Species), sum))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species total
1 5.1 3.5 1.4 0.2 setosa 10.2
2 4.9 3.0 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.0 3.6 1.4 0.2 setosa 10.2
或者,您可以使用select_if(., is.numeric)
而不是select(., -Species)
来进行更通用化(但如果计算中不包含某些数字变量,则无法使用它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.