行总和作为 R 中 table 中的额外列
Row sum as am extra column in the table in R
我正在尝试做一个描述性的 table。我首先计算每组和每年的观察次数。然后,我想添加另一列,其中包含每年的观察总和。
如何在不使用合并功能的情况下执行此操作?
year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
dta <- data.frame(year = year, group = group, value = value)
library(dplyr)
library(tidyr)
dta1 <- dta %>%
group_by(year, group) %>%
summarize(nobs = n()) %>%
pivot_wider(names_from= group, values_from = nobs)
dta2 <- dta %>%
group_by(year)%>%
summarize(total_nobs_per_year = n())
table <- merge(dta1, dta2, by="year")
table
我想要的 table 看起来像这样:
您可以使用基本的 R 函数 rowSums
从第二列开始,如下所示:
dta1$total_nobs_per_year<-rowSums(dta1[2:ncol(dta1)])
dta1
# A tibble: 2 x 9
# Groups: year [2]
year `0` `1` `2` `3` `4` `5` `6` total_nobs_per_year
<int> <int> <int> <int> <int> <int> <int> <int> <dbl>
1 2014 738 711 712 709 656 750 724 5000
2 2015 723 711 767 731 659 745 664 5000
根据定义,您的 total_nobs_per_year
将是 dta1
的行的总和,不包括第一列。您可以使用
dta1 %>%
ungroup() %>%
mutate(total_nobs_per_year = rowSums(dta1[-1]))
产生
# A tibble: 2 x 9
year `0` `1` `2` `3` `4` `5` `6` total_nobs_per_year
<int> <int> <int> <int> <int> <int> <int> <int> <dbl>
1 2014 683 699 722 731 701 712 752 5000
2 2015 704 689 734 706 726 709 732 5000
由于您的数据框一开始就采用整洁(长)格式,因此您还可以在透视之前计算总和,从而无需两个表。只需使用 mutate
而不是 summarise
来保留所有行:
library(dplyr)
library(tidyr)
year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
dta <- data.frame(year = year, group = group, value = value)
dta %>%
group_by(year, group) %>%
summarise(nobs = n(), .groups = "drop_last") %>%
mutate(total_nobs_per_year = sum(nobs)) %>%
pivot_wider(names_from = group, values_from = nobs)
#> # A tibble: 2 x 9
#> # Groups: year [2]
#> year total_nobs_per_year `0` `1` `2` `3` `4` `5` `6`
#> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 2014 5000 751 745 701 690 716 683 714
#> 2 2015 5000 741 737 706 632 694 746 744
之所以可行,是因为 mutate()
中的计算也是按组完成的(如 summarise
)。如果您想在不折叠组的情况下添加摘要列,这将非常有用。
我正在尝试做一个描述性的 table。我首先计算每组和每年的观察次数。然后,我想添加另一列,其中包含每年的观察总和。
如何在不使用合并功能的情况下执行此操作?
year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
dta <- data.frame(year = year, group = group, value = value)
library(dplyr)
library(tidyr)
dta1 <- dta %>%
group_by(year, group) %>%
summarize(nobs = n()) %>%
pivot_wider(names_from= group, values_from = nobs)
dta2 <- dta %>%
group_by(year)%>%
summarize(total_nobs_per_year = n())
table <- merge(dta1, dta2, by="year")
table
我想要的 table 看起来像这样:
您可以使用基本的 R 函数 rowSums
从第二列开始,如下所示:
dta1$total_nobs_per_year<-rowSums(dta1[2:ncol(dta1)])
dta1
# A tibble: 2 x 9
# Groups: year [2]
year `0` `1` `2` `3` `4` `5` `6` total_nobs_per_year
<int> <int> <int> <int> <int> <int> <int> <int> <dbl>
1 2014 738 711 712 709 656 750 724 5000
2 2015 723 711 767 731 659 745 664 5000
根据定义,您的 total_nobs_per_year
将是 dta1
的行的总和,不包括第一列。您可以使用
dta1 %>%
ungroup() %>%
mutate(total_nobs_per_year = rowSums(dta1[-1]))
产生
# A tibble: 2 x 9
year `0` `1` `2` `3` `4` `5` `6` total_nobs_per_year
<int> <int> <int> <int> <int> <int> <int> <int> <dbl>
1 2014 683 699 722 731 701 712 752 5000
2 2015 704 689 734 706 726 709 732 5000
由于您的数据框一开始就采用整洁(长)格式,因此您还可以在透视之前计算总和,从而无需两个表。只需使用 mutate
而不是 summarise
来保留所有行:
library(dplyr)
library(tidyr)
year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
dta <- data.frame(year = year, group = group, value = value)
dta %>%
group_by(year, group) %>%
summarise(nobs = n(), .groups = "drop_last") %>%
mutate(total_nobs_per_year = sum(nobs)) %>%
pivot_wider(names_from = group, values_from = nobs)
#> # A tibble: 2 x 9
#> # Groups: year [2]
#> year total_nobs_per_year `0` `1` `2` `3` `4` `5` `6`
#> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 2014 5000 751 745 701 690 716 683 714
#> 2 2015 5000 741 737 706 632 694 746 744
之所以可行,是因为 mutate()
中的计算也是按组完成的(如 summarise
)。如果您想在不折叠组的情况下添加摘要列,这将非常有用。