r - R-如何使用多个变量/列将 Long 重塑为 Wide
问题描述
我从以下数据子集开始
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
解决方案
有些变量可以更好地放在一起
df %>%
pivot_wider(id_cols = c(UserID, Full.Name, DOB, EncounterID), names_from = c(QuestionID, QName, labelnospaces), values_from = responses)
UserID Full.Name DOB EncounterID `505_Intro_Were you given any info?` `506_Care_By using this service..`
<int> <chr> <chr> <int> <chr> <chr>
1 1 John Smith 1-1-90 13 yes yes
2 2 Jane Doe 2-2-80 14 no no
`507_Out_How satisfied are you?`
<chr>
1 vsat
2 unsat
推荐阅读
- html - 如何将 bootstrap 5 默认菜单更改为画布外菜单?
- angular-material - 角材料反应形式。根据下拉选择禁用复选框
- consul - 在 Nomad UI 中找不到 Nomad 客户端
- javascript - javascript – 对参考书目的多个级别的数组进行排序
- apache - Htaccess 用参数值重写
- python - python程序创建了一个谷歌幻灯片演示,但结果是空的
- c# - 通过帖子将值传递给部分视图
- nuxt.js - 在 Nuxt 中使用自定义 webpack 加载器
- c - 预期的 struct foo* 但参数是函数指针的 struct foo* 类型
- python - Django 从 postgres JSON 字段中获取值