我目前有这个数据框:
sex Dose weekReceived
1 M 1 1
2 F 2 2
3 M 3 2
4 M 1 3
5 M 1 2
6 F 2 1
7 M 1 1
我想要这个数据框:
weekReceived Dose M F
1 1 1 2 0
2 1 2 0 1
3 2 1 1 0
4 2 2 0 1
5 2 3 1 0
6 3 1 1 0
为解释第二个数据帧,我要做的是计算每周接受每种剂量的男性和女性的数量(例如,第二个数据帧中的第1行告诉我们,两个男性在第一周)。
我要使用哪种功能来重新格式化?
答案 0 :(得分:0)
我们得到所有列的count
,以及spread
的“性别”,其频率列为“ n”至“宽”格式
library(dplyr)
df1 %>%
count(weekReceived, Dose, sex) %>%
spread(sex, n, fill = 0)
# A tibble: 6 x 4
# weekReceived Dose F M
# <int> <int> <dbl> <dbl>
#1 1 1 0 2
#2 1 2 1 0
#3 2 1 0 1
#4 2 2 1 0
#5 2 3 0 1
#6 3 1 0 1
df2 <- structure(list(sex = c("M", "F", "M", "M", "M", "F", "M"), Dose = c(1L,
2L, 3L, 1L, 1L, 2L, 1L), weekReceived = c(1L, 2L, 2L, 3L, 2L,
1L, 1L)), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7"))