dplyr:为什么单个计数摘要和索引摘要不同

dplyr:为什么单个计数摘要和索引摘要不同,r,dplyr,R,Dplyr,我正在创建一个新列,其中包含一个函数中的分组摘要计数。为什么: iris %>% group_by(Species) %>% mutate(Count = sum(Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)) 不产生与相同的结果 iris %>% mutate(count = sum(.[1:ncol(.)]) 或 如何使用列索引创建计数和,以便插入到具有可变列名称的函数中?(编制索引的原始原

我正在创建一个新列,其中包含一个函数中的分组摘要计数。为什么:

iris %>% 
  group_by(Species) %>% 
  mutate(Count = sum(Sepal.Length + Sepal.Width + Petal.Length + Petal.Width))
不产生与相同的结果

iris %>% mutate(count = sum(.[1:ncol(.)])


如何使用列索引创建计数和,以便插入到具有可变列名称的函数中?(编制索引的原始原因)

一种方法是在
分组后
嵌套
,使用
map
循环嵌套的“数据”,选择数字列(
select\u if
),
通过获取
rowSums
sum
,以及
unest

library(tidyverse)
iris %>% 
  group_by(Species) %>% 
  nest %>%
  mutate(data = map(data, ~ .x %>% 
                              select_if(is.numeric) %>% 
                              mutate(Count = sum(rowSums(.))))) %>% 
                              #or use reduce with sum
                              # mutate(Count = reduce(., `+`) %>% sum))) %>%
  unnest 
# A tibble: 150 x 6
#   Species Sepal.Length Sepal.Width Petal.Length Petal.Width Count
#   <fct>          <dbl>       <dbl>        <dbl>       <dbl> <dbl>
# 1 setosa           5.1         3.5          1.4         0.2  507.
# 2 setosa           4.9         3            1.4         0.2  507.
# 3 setosa           4.7         3.2          1.3         0.2  507.
# 4 setosa           4.6         3.1          1.5         0.2  507.
# 5 setosa           5           3.6          1.4         0.2  507.
# 6 setosa           5.4         3.9          1.7         0.4  507.
# 7 setosa           4.6         3.4          1.4         0.3  507.
# 8 setosa           5           3.4          1.5         0.2  507.
# 9 setosa           4.4         2.9          1.4         0.2  507.
#10 setosa           4.9         3.1          1.5         0.1  507.
# ... with 140 more rows
库(tidyverse)
虹膜%>%
组别(种类)%>%
嵌套%>%
变异(数据=映射(数据,~.x%>%
如果(是数值)%>%,请选择
突变(计数=总和(行总和(.щщ))%>%
#或者用总和来减少
#突变(计数=减少(,`+`)%>%sum))%>%
不耐烦
#一个tibble:150x6
#种萼片。长萼片。宽花瓣。长花瓣。宽计数
#                                    
#1 setosa 5.1 3.5 1.4 0.2 507。
#2 setosa 4.9 3 1.4 0.2 507。
#3 setosa 4.7 3.2 1.3 0.2 507。
#4 setosa 4.6 3.1 1.5 0.2 507。
#5 setosa 5 3.6 1.4 0.2 507。
#6 setosa 5.4 3.9 1.7 0.4 507。
#7 setosa 4.6 3.4 1.4 0.3 507。
#8 setosa 5 3.4 1.5 0.2 507。
#9 setosa 4.4 2.9 1.4 0.2 507。
#10 setosa 4.9 3.1 1.5 0.1 507。
# ... 还有140多行

您需要
rowSums
然后用
sum
换行,即
sum(rowSums()
如果存在数值以外的列,最好使用mutate\u if()?
mutate\u if
将循环遍历每个列并更新现有列或创建新列。我想你只是对创建一个专栏感兴趣。在这种情况下,可能您需要
map_if
我已经添加了带有iris的reprex,我相信我的语法是关闭的,但也无法使用map_if(is.numeric,mutate(count=sum)(rowSums(.))获得它。谢谢您添加reprex谢谢-这肯定对添加列起到了作用。我相信是因为双重mutate(mutate())它还复制了现有列-有什么解决方法吗?分组列可以直接传递到
nest()
iris%>%nest(-Species)%%>%…
No
group\u by
required.@CoreyPembleton.这是我以前的案例。我现在编辑了你能检查一下吗?是的,早些时候我错误地在第一个
mutate
callYes中添加了
count
而不是
data
,我明白了-更改为data允许数据部分在嵌套时使用。谢谢你的帮助,th这件事困扰了我一段时间
library(tidyverse)
iris %>% 
  group_by(Species) %>% 
  nest %>%
  mutate(data = map(data, ~ .x %>% 
                              select_if(is.numeric) %>% 
                              mutate(Count = sum(rowSums(.))))) %>% 
                              #or use reduce with sum
                              # mutate(Count = reduce(., `+`) %>% sum))) %>%
  unnest 
# A tibble: 150 x 6
#   Species Sepal.Length Sepal.Width Petal.Length Petal.Width Count
#   <fct>          <dbl>       <dbl>        <dbl>       <dbl> <dbl>
# 1 setosa           5.1         3.5          1.4         0.2  507.
# 2 setosa           4.9         3            1.4         0.2  507.
# 3 setosa           4.7         3.2          1.3         0.2  507.
# 4 setosa           4.6         3.1          1.5         0.2  507.
# 5 setosa           5           3.6          1.4         0.2  507.
# 6 setosa           5.4         3.9          1.7         0.4  507.
# 7 setosa           4.6         3.4          1.4         0.3  507.
# 8 setosa           5           3.4          1.5         0.2  507.
# 9 setosa           4.4         2.9          1.4         0.2  507.
#10 setosa           4.9         3.1          1.5         0.1  507.
# ... with 140 more rows