以下是输入数据:一张提供年龄分类(年龄段-单个年龄年份或年龄范围)和20人性别的表格。
library(tidyverse)
df <- tibble(agestrat = c("5", "6", "6", "7", "7", "7", "7", "9", "11", "11", "12", "14", "17", "18", "18", "22", "24", "25-40", "41-50", "51-60"),
gender = c("male", "female", "female", "male", "male", "male", "female", "female", "male", "female", "female", "male", "female", "female", "male", "male", "female", "male", "male", "female"))
这是我想要的输出:
agestrat male female
5 1
6 2
7 3 1
9 1
11 1 1
12 1
14 1
17 1
18 1 1
22 1
24 1
25-40 1
41-50 1
51-60 1
一个汇总了整个Agestrat并提供每个Agestrat中男性和女性人数的表格。我在dplyr
中尝试了多种方法,包括group_by
,summarise
和nest
,但是我对如何创建{{1 }}和male
,并在每个单元格中插入必需的计数。
谢谢!
答案 0 :(得分:-1)
我们可以使用count
获取频率,然后spread
将其转换为“宽”格式
library(tidyverse)
df %>%
count(agestrat, gender) %>%
spread(gender, n, fill = 0)