我有一个这样的数据框:
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(),但这还不足以满足我的需求。我需要将每所学校同一年级的教室数量和每所学校同一年级的学生人数相加。