【发布时间】:2021-10-06 04:25:50
【问题描述】:
我从以下数据子集开始
UserID labelnospaces responses
1 Were you given any info? yes
1 By using this service..? yes
1 How satisfied are you? Very satisfied
2 Were you given any info? no
2 By using this service..? no
2 How satisfied are you? unsatisfied
通过使用下面的代码,我能够完美地从长到宽
service_L_to_W<- reshape(data=service, idvar="UserID",
timevar = "labelnospaces",
direction = "wide")
使用上面的代码,我得到了(这就是我想要的)
UserID Were you given any info? By using this service..? How satisfied are you?
1 yes yes very satisfied
2 no no unsatisfied
我的问题是如何编辑我的代码,以便我可以将我的数据(带有额外的变量/列)从长转换为宽:
UserID Full Name DOB EncounterID QuestionID Name Type labelnospaces responses
1 John Smith 1-1-90 13 505 Intro Check Were you given any info? yes
1 John Smith 1-1-90 13 506 Care Check By using this service.. yes
1 John Smith 1-1-90 13 507 Out Check How satisfied are you? vsat
2 Jane Doe 2-2-80 14 505 Intro Check Were you given any info? no
2 Jane Doe 2-2-80 14 506 Care Check By using this service.. no
2 Jane Doe 2-2-80 14 507 Out Check How satisfied are you? unsat
【问题讨论】:
-
您的预期输出效果如何?你在找
tidyr::pivot_wider(df, id_cols = c(UserID, Full.Name, DOB, EncounterID, Type), names_from = labelnospaces, values_from = responses) -
@RonakShah 谢谢!