[英]R how to create table from dataframe containing counts over groups?
I have a dataframe with the count of for instance males and females for certain groups arranged in this way:我有一个 dataframe 以这种方式排列的某些组的例如男性和女性的数量:
df <- data.frame (
Round = c("R1", "R1", "R2", "R2"),
N. = c(20, 10, 15,15),
Gender = c("M", "F", "M","F"))
How can I create a table accounting for counts over, for instance, Round and Gender?我怎样才能创建一个表来计算,例如,Round 和 Gender? I would like to show the distribution of gender for each round.
我想展示每一轮的性别分布。
I have tried我努力了
table (df$Gender, df$Round)
but this is not what I need.但这不是我需要的。 I need instead to show N. by groups.
相反,我需要按组显示 N.。
Something like this?是这样的吗?
library(tidyr)
pivot_wider(df, names_from = Round, values_from = N.)
Gender R1 R2
1 M 20 15
2 F 10 15
Or in base R with reshape
:或者在 base R 中
reshape
:
reshape(df, direction = "wide", idvar = "Gender", timevar = "Round")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.