![](/img/trans.png)
[英]R code that will group_by multiple columns and summarize when a data element appears across the column group
[英]Summarize by group and across multiple columns in R
我有一個數據框,其中包含學生的一些基本信息。 我想獲得關於年齡、性別和群體的匯總統計數據。
set.seed(500)
testdf <- data.frame(ID = paste0("Stu", c(1:10)),
Age = sample(18:25, 10, replace = T),
Sex = sample(c("Boy", "Girl", "NA"), 10, replace = T),
Name = c("Pwyll","Flavian","Leehi","Zuzana","Aniya","Bogomil"
,"Lameez","Prudencia","Ikuo","Grayson"),
GroupMath = sample(LETTERS[1:2], 10, replace = T),
GroupEng = sample(LETTERS[1:2], 10, replace = T),
GroupScie = sample(LETTERS[1:2], 10, replace = T),
GroupChine = sample(LETTERS[1:2], 10, replace = T))
我希望它看起來像這張照片。(所需的輸出。)
在我的代碼中,我使用三個部分來處理 GroupMath,然后是 GroupEng,然后是 GroupScie,然后是 GroupChine。 有誰知道我怎樣才能提高效率? 我怎么感謝你都不為過。
N.math <- testdf %>% group_by(GroupMath) %>% count(GroupMath)
Age.math <- testdf %>% group_by(GroupMath) %>% summarize(
Mean = mean(Age),
Max = max(Age),
Min = min(Age),
sd = sd(Age))
Sex.math <- testdf %>% group_by(GroupMath) %>% count(Sex)
這里至少有一種方法可以簡化您的匯總統計數據,這樣您就不會一個一個地匯總每個組。 首先,您可以使用組和類作為目標變量將數據轉換為長格式,然后匯總這些組的數據。 首先,支點:
#### Load Tidyverse ####
library(tidyverse)
#### Pivot to Long Format ####
groups <- testdf %>%
pivot_longer(cols = contains("Group"),
names_to = "Class",
values_to = "Group")
groups
看起來像這樣:
# A tibble: 40 × 6
ID Age Sex Name Class Group
<chr> <int> <chr> <chr> <chr> <chr>
1 Stu1 24 Girl Pwyll GroupMath B
2 Stu1 24 Girl Pwyll GroupEng B
3 Stu1 24 Girl Pwyll GroupScie A
4 Stu1 24 Girl Pwyll GroupChine B
5 Stu2 20 Girl Flavian GroupMath B
6 Stu2 20 Girl Flavian GroupEng A
7 Stu2 20 Girl Flavian GroupScie A
8 Stu2 20 Girl Flavian GroupChine B
9 Stu3 24 NA Leehi GroupMath A
10 Stu3 24 NA Leehi GroupEng B
然后您可以使用類、組和性別聚合數據:
#### Aggregate Data by Class x Group ####
sums <- groups %>%
group_by(Class,Group,Sex) %>%
summarise(
Mean = mean(Age),
Max = max(Age),
Min = min(Age),
sd = sd(Age)) %>%
ungroup()
sums
如下所示。 請注意,某些值是 NA,因為在某些情況下每個性別只有一個人,因此在這種情況下不存在標准偏差:
# A tibble: 16 × 7
Class Group Sex Mean Max Min sd
<chr> <chr> <chr> <dbl> <int> <int> <dbl>
1 GroupChine A Boy 24 24 24 NA
2 GroupChine A Girl 22.3 24 21 1.53
3 GroupChine B Girl 20 24 18 2.35
4 GroupChine B NA 24 24 24 NA
5 GroupEng A Boy 24 24 24 NA
6 GroupEng A Girl 20.5 24 19 2.38
7 GroupEng B Girl 21.2 24 18 2.5
8 GroupEng B NA 24 24 24 NA
9 GroupMath A Girl 20.8 24 19 2.36
10 GroupMath A NA 24 24 24 NA
11 GroupMath B Boy 24 24 24 NA
12 GroupMath B Girl 21 24 18 2.58
13 GroupScie A Girl 21.7 24 20 2.08
14 GroupScie A NA 24 24 24 NA
15 GroupScie B Boy 24 24 24 NA
16 GroupScie B Girl 20.4 24 18 2.51
然后你可以像這樣得到性別計數:
#### Get Grouped Gender Counts ####
sex <- groups %>%
group_by(Class,Group) %>%
count(Sex) %>%
ungroup()
sex
看起來像這樣:
# A tibble: 16 × 4
Class Group Sex n
<chr> <chr> <chr> <int>
1 GroupChine A Boy 1
2 GroupChine A Girl 3
3 GroupChine B Girl 5
4 GroupChine B NA 1
5 GroupEng A Boy 1
6 GroupEng A Girl 4
7 GroupEng B Girl 4
8 GroupEng B NA 1
9 GroupMath A Girl 4
10 GroupMath A NA 1
11 GroupMath B Boy 1
12 GroupMath B Girl 4
13 GroupScie A Girl 3
14 GroupScie A NA 1
15 GroupScie B Boy 1
16 GroupScie B Girl 5
最后,您可以通過這種方式加入這兩個數據框:
#### Join ####
sums %>%
right_join(sex)
給你最終的產品。 您現在可以看到 NA 值的來源,例如僅包含 1 個男孩的第 1 行,使得 SD 無法評估:
Joining, by = c("Class", "Group", "Sex")
# A tibble: 16 × 8
Class Group Sex Mean Max Min sd n
<chr> <chr> <chr> <dbl> <int> <int> <dbl> <int>
1 GroupChine A Boy 24 24 24 NA 1
2 GroupChine A Girl 22.3 24 21 1.53 3
3 GroupChine B Girl 20 24 18 2.35 5
4 GroupChine B NA 24 24 24 NA 1
5 GroupEng A Boy 24 24 24 NA 1
6 GroupEng A Girl 20.5 24 19 2.38 4
7 GroupEng B Girl 21.2 24 18 2.5 4
8 GroupEng B NA 24 24 24 NA 1
9 GroupMath A Girl 20.8 24 19 2.36 4
10 GroupMath A NA 24 24 24 NA 1
11 GroupMath B Boy 24 24 24 NA 1
12 GroupMath B Girl 21 24 18 2.58 4
13 GroupScie A Girl 21.7 24 20 2.08 3
14 GroupScie A NA 24 24 24 NA 1
15 GroupScie B Boy 24 24 24 NA 1
16 GroupScie B Girl 20.4 24 18 2.51 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.