汇总行并将行转置为 R 中的列

Summarize and Transpose rows to columns in R

这是我的输入数据:

Program = c("A","A","A","B","B","C")
Age = c(10,30,30,12,32,53)
Gender = c("F","F","M","M","M","F")
Language = c("Eng","Eng","Kor","Kor","Other","Other")
df = data.frame(Program,Age,Gender,Language)

我想输出一个 table 这样的:

Program MEAN AGE ENG KOR FEMALE MALE
A
B
C

其中 MEAN AGE 是平均年龄,ENG,KOR,FEMALE,MALE 是计数。

我尝试过使用 dplyr 和 t(),但在这种情况下,我觉得我完全不知道步骤是什么(我的第一个 post,这是新手)。提前致谢!

您可以采取以下方法:

library(dplyr)

df %>%
  group_by(Program) %>%
  summarise(
    `Mean Age` = mean(Age),
    ENG = sum(Language=="Eng"),
    KOR = sum(Language=="Kor"),
    Female = sum(Gender=="F"),
    Male = sum(Gender=="M"),
    .groups="drop"
  )

输出:

# A tibble: 3 x 6
  Program `Mean Age`   ENG   KOR Female  Male
  <chr>        <dbl> <int> <int>  <int> <int>
1 A             23.3     2     1      2     1
2 B             22       0     1      0     2
3 C             53       0     0      1     0

注: .groupsdplyr函数的特殊变量。这里的使用方式相当于计算后使用%>% ungroup()。如果您在 summarise 函数中键入任何其他名称,它将假定它是一个列名。

在基础 R 中你可以这样做:

df1 <- cbind(df[1:2], stack(df[3:4])[-2])
cbind(aggregate(Age~Program, df, mean),as.data.frame.matrix(table(df1[-2])))
  Program      Age Eng F Kor M Other
A       A 23.33333   2 2   1 1     0
B       B 22.00000   0 0   1 2     1
C       C 53.00000   0 1   0 0     1