我有以下示例数据集:
data.frame(SEX=c("M","F","M","F"),COMPLAINT=c("headache","headache", "dizziness", "dizziness"),
reports=c(5,4,9,12), users = c(1250,3460,2500,1850))
SEX COMPLAINT reports users
1 M headache 5 1250
2 F headache 4 3460
3 M dizziness 9 2500
4 F dizziness 12 1850
我的问题是如何合并行1和2以及3和4,以便我的数据框如下:
COMPLAINT reports_male reports_female users_male users_female
1 headache 5 4 1250 3460
2 dizziness 9 12 2500 1850
任何人都有一个快速的解决方案,我可以用于(更大)更大的数据集?
答案 0 :(得分:3)
我们可以使用dcast
中的data.table
,value.var
可以使用多个library(data.table)
dcast(setDT(df1), COMPLAINT ~ SEX, value.var = c("reports", "users"))
# COMPLAINT reports_F reports_M users_F users_M
#1: dizziness 12 9 1850 2500
#2: headache 4 5 3460 1250
列,并且对大数据集非常有效
FIRDataSnapshot
答案 1 :(得分:2)
如How to reshape data from long to wide format?所示,我们可以使用library(reshape2)
然后
reshape(df, idvar = "COMPLAINT", timevar = "SEX", direction = "wide")
。
COMPLAINT reports.M users.M reports.F users.F
1 headache 5 1250 4 3460
3 dizziness 9 2500 12 1850