[英]Summarize and Transpose rows to columns in R
This is my input data:这是我的输入数据:
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)
I would like to output a table like this:我想 output 像这样的表:
Program![]() |
MEAN AGE![]() |
ENG![]() |
KOR![]() |
FEMALE![]() |
MALE![]() |
---|---|---|---|---|---|
A![]() |
|||||
B![]() |
|||||
C ![]() |
Where MEAN AGE is the average age, ENG,KOR,FEMALE,MALE are counts. MEAN AGE 是平均年龄,ENG,KOR,FEMALE,MALE 是计数。
I have tried using dplyr and t() but in this case I feel like I'm completely lost as to what are the steps (my first post, new to this).我曾尝试使用 dplyr 和 t() 但在这种情况下,我觉得我完全不知道步骤是什么(我的第一篇文章,新的)。 Thank you in advance!
先感谢您!
You can take the following approach:您可以采取以下方法:
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"
)
Output: Output:
# 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
Note: .groups
is a special variable for dplyr
functions.注意:
.groups
是dplyr
函数的特殊变量。 The way it's used here is equivalent to using %>% ungroup()
after the calculation.这里使用的方式相当于在计算后使用
%>% ungroup()
。 If you type any other name in the summarise
function, it will assume it's a column name.如果您在
summarise
function 中键入任何其他名称,它将假定它是列名称。
In base R you could do:在基础 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.