分组摘要/子集dplyr

时间:2017-02-08 20:44:25

标签: r dplyr aggregate summarize

我有两个不同学期的两门课程的数据集,采用以下形式:

set.seed(200)
sem <- sample(c("1", "2"), 200, replace = T)
course <- sample(c("1", "2"), 200, replace = T)
d.gender = sample(c(0, 1), 200, replace = T, prob = c(0.6, 0.4))
d.pass = sample(c(0, 1), 200, replace = T, prob = c(0.7, 0.3))
df <- data.frame(sem, course, d.gender, d.pass)

我试图有效地创建4种不同的sem,课程组合及其总合格率,d.gender的百分比= 1,最后通过率在这2个性别类别中。我可以创建一个表,提供我需要计算的所有值,但我知道有一种更有效的方法来计算我需要的东西而不需要嵌套一堆不同的group_by和summary函数,或者制作一大堆不同的tbls和left_joining我想要的列。我可以通过索引和子集函数得到我需要的东西,但是我希望有一种更好的方法来获得我需要的所有东西的4行矩阵,但它很难看并且需要永远,并且它很容易在代码中出错:

df1 <- df %>% group_by(sem, course, d.gender, d.pass) %>% summarize(total = n())
df1$total_pass <- rep(NA, dim(df1)[1])
df1$total_pass[1:4] <- sum(subset(df1, sem == "1" & course == "1" & d.pass == "1", 
    select = total))
df1$total_pass[5:8] <- sum(subset(df1, sem == "1" & course == "2" & d.pass == "1", 
    select = total))
df1$total_pass[9:12] <- sum(subset(df1, sem == "2" & course == "1" & d.pass == "1", 
    select = total))
df1$total_pass[13:16] <- sum(subset(df1, sem == "2" & course == "2" & d.pass == "1", 
    select = total))

df1$n_male <- rep(NA, dim(df1)[1])
df1$n_male[1:4] <- sum(subset(df1, sem == "1" & course == "1" & d.gender == "1", 
    select = total))
df1$n_male[5:8] <- sum(subset(df1, sem == "1" & course == "2" & d.gender == "1", 
    select = total))
df1$n_male[9:12] <- sum(subset(df1, sem == "2" & course == "1" & d.gender == "1", 
    select = total))
df1$n_male[13:16] <- sum(subset(df1, sem == "2" & course == "2" & d.gender == "1", 
    select = total))

df1$n_fem <- rep(NA, dim(df1)[1])
df1$n_fem[1:4] <- sum(subset(df1, sem == "1" & course == "1" & d.gender == "0", select = total))
df1$n_fem[5:8] <- sum(subset(df1, sem == "1" & course == "2" & d.gender == "0", select = total))
df1$n_fem[9:12] <- sum(subset(df1, sem == "2" & course == "1" & d.gender == "0", 
    select = total))
df1$n_fem[13:16] <- sum(subset(df1, sem == "2" & course == "2" & d.gender == "0", 
    select = total))

df1$pct_male <- rep(NA, dim(df1)[1])
df1$pct_male[1:4] <- df1$n_male[1:4]/sum(subset(df1, sem == "1" & course == "1", 
    select = total))
df1$pct_male[5:8] <- df1$n_male[5:8]/sum(subset(df1, sem == "1" & course == "2", 
    select = total))
df1$pct_male[9:12] <- df1$n_male[9:12]/sum(subset(df1, sem == "2" & course == "1", 
    select = total))
df1$pct_male[13:16] <- df1$n_male[13:16]/sum(subset(df1, sem == "2" & course == "2", 
    select = total))

df1$pct_fem <- rep(NA, dim(df1)[1])
df1$pct_fem <- 1 - df1$pct_male

df1$pct_pass <- rep(NA, dim(df1)[1])
df1$pct_pass[1:4] <- df1$total_pass[1:4]/sum(subset(df1, sem == "1" & course == "1", 
    select = total))
df1$pct_pass[5:8] <- df1$total_pass[5:8]/sum(subset(df1, sem == "1" & course == "2", 
    select = total))
df1$pct_pass[9:12] <- df1$total_pass[9:12]/sum(subset(df1, sem == "2" & course == 
    "1", select = total))
df1$pct_pass[13:16] <- df1$total_pass[13:16]/sum(subset(df1, sem == "2" & course == 
    "2", select = total))

df1$male_pass_pct <- rep(NA, dim(df1)[1])
df1$male_pass_pct[1:4] <- subset(df1, sem == "1" & course == "1" & d.gender == "1" & 
    d.pass == "1", select = total)/df1$n_male[1:4]
df1$male_pass_pct[5:8] <- subset(df1, sem == "1" & course == "2" & d.gender == "1" & 
    d.pass == "1", select = total)/df1$n_male[5:8]
df1$male_pass_pct[9:12] <- subset(df1, sem == "2" & course == "1" & d.gender == "1" & 
    d.pass == "1", select = total)/df1$n_male[9:12]
df1$male_pass_pct[13:16] <- subset(df1, sem == "2" & course == "2" & d.gender == 
    "1" & d.pass == "1", select = total)/df1$n_male[13:16]

df1$fem_pass_pct <- rep(NA, dim(df1)[1])
df1$fem_pass_pct[1:4] <- subset(df1, sem == "1" & course == "1" & d.gender == "0" & 
    d.pass == "1", select = total)/df1$n_fem[1:4]
df1$fem_pass_pct[5:8] <- subset(df1, sem == "1" & course == "2" & d.gender == "0" & 
    d.pass == "1", select = total)/df1$n_fem[5:8]
df1$fem_pass_pct[9:12] <- subset(df1, sem == "2" & course == "1" & d.gender == "0" & 
    d.pass == "1", select = total)/df1$n_fem[9:12]
df1$fem_pass_pct[13:16] <- subset(df1, sem == "2" & course == "2" & d.gender == "0" & 
    d.pass == "1", select = total)/df1$n_fem[13:16]


df2 <- df1 %>% 
    group_by(sem, course) %>% 
    summarize(total_pass = first(total_pass), 
              pct_pass = first(pct_pass), 
              n_male = first(n_male), 
              n_fem = first(n_fem), 
              pct_male = first(pct_male), 
              pct_fem = first(pct_fem), 
              male_pass_pct = first(male_pass_pct), 
              fem_pass_pct = first(fem_pass_pct))

df2 <- unique(df1[, c(1, 2, 6, 11, 7:10, 12, 13)])
df2[, c(9, 10)] <- lapply(df2[, c(9, 10)], as.numeric)

对于只需要4行的测量而言真的很费力,但是我不能让它为这个聚合工作......任何帮助都会很棒

1 个答案:

答案 0 :(得分:2)

只需分组,然后summarise原始。您可以使用n()来引用组中的行数,并可以引用先前在summarise中创建的变量,这样可以让您执行

df %>% group_by(sem, course) %>% 
    summarise(total_pass = sum(d.pass), 
              n_male = sum(d.gender), 
              n_fem = sum(d.gender == 0), 
              pct_male = n_male / n(), 
              pct_fem = n_fem / n(), 
              pct_pass = total_pass / n(), 
              male_pass_pct = sum(d.gender & d.pass) / n_male, 
              fem_pass_pct = sum(d.gender == 0 & d.pass) / n_fem)

## Source: local data frame [4 x 10]
## Groups: sem [?]
## 
##      sem course total_pass n_male n_fem  pct_male   pct_fem  pct_pass male_pass_pct fem_pass_pct
##   <fctr> <fctr>      <dbl>  <dbl> <int>     <dbl>     <dbl>     <dbl>         <dbl>        <dbl>
## 1      1      1         14     20    30 0.4000000 0.6000000 0.2800000    0.25000000    0.3000000
## 2      1      2          7     19    26 0.4222222 0.5777778 0.1555556    0.05263158    0.2307692
## 3      2      1         12     23    23 0.5000000 0.5000000 0.2608696    0.30434783    0.2173913
## 4      2      2         16     25    34 0.4237288 0.5762712 0.2711864    0.20000000    0.3235294

如果您愿意,重塑数据以将性别从列标题移动到实际变量将使您的数据更加整洁,并且需要更少的操作。