R 将组大小添加到汇总表不再有效
我正在聚合一些数据,我想将组大小R 将组大小添加到汇总表不再有效,r,dplyr,R,Dplyr,我正在聚合一些数据,我想将组大小N添加到输出表中。直到最近,下面的代码仍然运行良好。现在,N等于我的表的行数 iris %>% group_by(Species) %>% group_by(N = n(), .add = TRUE) %>% summarise_all(list(~mean(., na.rm = TRUE))) # A tibble: 3 x 6 # Groups: Species [3] Species N Sepa
N
添加到输出表中。直到最近,下面的代码仍然运行良好。现在,N
等于我的表的行数
iris %>%
group_by(Species) %>%
group_by(N = n(), .add = TRUE) %>%
summarise_all(list(~mean(., na.rm = TRUE)))
# A tibble: 3 x 6
# Groups: Species [3]
Species N Sepal.Length Sepal.Width Petal.Length Petal.Width
<fct> <int> <dbl> <dbl> <dbl> <dbl>
1 setosa 150 5.01 3.43 1.46 0.246
2 versicolor 150 5.94 2.77 4.26 1.33
3 virginica 150 6.59 2.97 5.55 2.03
iris%>%
组别(种类)%>%
分组依据(N=N(),.add=TRUE)%>%
总结所有(列表(~mean(,na.rm=TRUE)))
#一个tibble:3x6
#类群:种[3]
种萼片。长萼片。宽花瓣。长花瓣。宽
1 setosa 150 5.01 3.43 1.46 0.246
2花色150 5.94 2.77 4.26 1.33
3维吉尼亚150 6.59 2.97 5.55 2.03
这看起来像是最近引入的错误。可在dplyr
1.0.3上复制,但不能在1.0.2上复制
但是,在这种情况下,您可以通过完全避免第二个组
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(across(.fns = mean, na.rm = TRUE),
N = n())
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width N
#* <fct> <dbl> <dbl> <dbl> <dbl> <int>
#1 setosa 5.01 3.43 1.46 0.246 50
#2 versicolor 5.94 2.77 4.26 1.33 50
#3 virginica 6.59 2.97 5.55 2.03 50
库(dplyr)
虹膜%>%
组别(种类)%>%
总结(跨越(.fns=平均值,na.rm=真实值),
N=N()
#种萼片。长萼片。宽花瓣。长花瓣。宽N
#*
#1 setosa 5.01 3.43 1.46 0.246 50
#2花色5.94 2.77 4.26 1.33 50
#3弗吉尼亚州6.59 2.97 5.55 2.03 50
这看起来像是最近引入的错误。可在dplyr
1.0.3上复制,但不能在1.0.2上复制
但是,在这种情况下,您可以通过
完全避免第二个组
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(across(.fns = mean, na.rm = TRUE),
N = n())
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width N
#* <fct> <dbl> <dbl> <dbl> <dbl> <int>
#1 setosa 5.01 3.43 1.46 0.246 50
#2 versicolor 5.94 2.77 4.26 1.33 50
#3 virginica 6.59 2.97 5.55 2.03 50
库(dplyr)
虹膜%>%
组别(种类)%>%
总结(跨越(.fns=平均值,na.rm=真实值),
N=N()
#种萼片。长萼片。宽花瓣。长花瓣。宽N
#*
#1 setosa 5.01 3.43 1.46 0.246 50
#2花色5.94 2.77 4.26 1.33 50
#3弗吉尼亚州6.59 2.97 5.55 2.03 50
试试这个:
rm(list = ls())
library(dplyr)
iris %>%
group_by(Species) %>%
group_by(N = n(), .add = TRUE) %>%
summarise_all(list(~mean(., na.rm = TRUE)))
试试这个:
rm(list = ls())
library(dplyr)
iris %>%
group_by(Species) %>%
group_by(N = n(), .add = TRUE) %>%
summarise_all(list(~mean(., na.rm = TRUE)))
上面的代码确实对我有用,并给出了正确的结果(每组N=50)。你重启了R吗?您正在加载哪些其他包?我得到的N
列为每个物种的50个。你的软件包版本('dplyr')
是什么?对我有用,但对我没有帮助。我只加载了tidyverse
软件包版本1.3.0。packageVersion('dplyr')
是1.0.3。上面的代码对我有效,并给出了正确的结果(每组N=50)。你重启了R吗?您正在加载哪些其他包?我得到的N
列为每个物种的50个。你的软件包版本('dplyr')
是什么?对我有用,但对我没有帮助。我只加载了tidyverse
软件包版本1.3.0。packageVersion('dplyr')
是1.0.3。