3

我有一个这样的数据框:

tibble(
  School = c(1, 1, 2, 3, 3, 4),
  City = c("A","A", "B", "C", "C", "B"),
  Grade = c("7th", "7th", "7th", "6th", "8th", "8th"),
  Number_Students = c(20, 23, 25, 21, 28, 34),
  Type_school = c("public", "public", "private", "public", "public", "private")
  )
ID 学校 城市 年级 Number_Students 类型_学校
1 1 一个 7日 20 上市
2 1 一个 7日 23 上市
3 2 7日 25 私人的
4 3 C 6日 21 上市
5 3 C 8日 28 上市
6 4 8日 34 私人的

分析单位是教室,但我想把它变成一个数据框,其中分析单位是学校,但有一些计算。像这样:

tibble(
  School = c(1, 2, 3, 4),
  City = c("A", "B", "C", "B"),
  N_6th = c(0, 0, 1, 0), # here is the number of grade 6h classrooms in each school 
  N_7th = c(2,1,0,0),
  N_8th = c(0,0,1,1),
  Students_6th = c(0, 0, 25, 0), # here is the number of students in grade 6th from each school (the sum of all 7th grade classrooms from each school)
  Students_7th = c(43, 25, 0, 0), 
  Students_8th = c(0, 0, 28, 34),
  Type_school = c("public", "private", "public", "private")
)
学校 城市 N_6th N_7th N_8th 学生_6th 学生_7th 学生_8th 类型_学校
1 一个 0 2 0 0 43 0 上市
2 0 1 0 0 25 0 私人的
3 C 1 0 1 25 0 28 上市
4 0 0 1 0 0 34 私人的

我正在尝试使用 pivot_wider(),但这还不足以满足我的需求。我需要将每所学校同一年级的教室数量和每所学校同一年级的学生人数相加。

4

2 回答 2

3

这是另一种方法:无法与 akrun 的完美方法相提并论,但它包含一些有趣的功能,我们可以如何获得相同的结果:

library(tidyr)
library(dplyr)

df1 <- df %>% 
  pivot_wider(id_cols = c(School, City, Grade, Type_school),
              names_from = "Grade",
              values_from = "Number_Students",
              values_fn = list(Number_Students = length),
              values_fill = 0,
              names_glue = "N_{Grade}") 

df %>% 
  pivot_wider(id_cols = c(School, City, Grade, Number_Students),
              names_from = Grade,
              values_from = Number_Students,
              values_fn = list(Number_Students = sum),
              names_glue = "Students_{Grade}"
              ) %>% 
  right_join(df1, by=c("School", "City"))
  School City  Students_7th Students_6th Students_8th Type_school N_7th N_6th N_8th
   <dbl> <chr>        <dbl>        <dbl>        <dbl> <chr>       <int> <int> <int>
1      1 A               43           NA           NA public          2     0     0
2      2 B               25           NA           NA private         1     0     0
3      3 C               NA           21           28 public          0     1     1
4      4 B               NA           NA           34 private         0     0     1
于 2022-01-21T21:07:48.737 回答
3

进行分组并返回计数和sum“Number_Students”,然后使用pivot_wider指定names_from为“等级”和values_from列向量

library(dplyr)
library(tidyr)
df1 %>%
  group_by(School, City, Grade, Type_school) %>%
  summarise(N = n(), Students = sum(Number_Students), .groups = 'drop') %>%
  pivot_wider(names_from = Grade, values_from = c(N, Students), values_fill = 0)

-输出

# A tibble: 4 × 9
  School City  Type_school N_7th N_6th N_8th Students_7th Students_6th Students_8th
   <dbl> <chr> <chr>       <int> <int> <int>        <dbl>        <dbl>        <dbl>
1      1 A     public          2     0     0           43            0            0
2      2 B     private         1     0     0           25            0            0
3      3 C     public          0     1     1            0           21           28
4      4 B     private         0     0     1            0            0           34
于 2022-01-21T20:02:20.137 回答