【发布时间】:2018-10-08 22:00:04
【问题描述】:
我有多个数据集,我使用 rbind 合并到 1 个 dplyr 数据帧中。
GapAnalysis16 <- select(memSat16,
importance_communication_website_content,
satisfaction_communication_website_content,
status,
Year2016) %>%
rename(ComImpt=importance_communication_website_content,
ComSat = satisfaction_communication_website_content,
status = status,
year = Year2016)
GapAnalysis17July <- select(memSatJuly17,
importance_communication_website_content_JULY17,
satisfaction_communication_website_content_JULY17,
role_primary_new_JULY17,Year2017_July) %>%
rename(ComImpt=importance_communication_website_content_JULY17,
ComSat = satisfaction_communication_website_content_JULY17,
status = role_primary_new_JULY17,
year = Year2017_July)
GapAnalysis <- rbind(GapAnalysis17July,GapAnalysis16)
得到了我的新组合数据集:
ComImpt ComSat status year
1 4 2 1 1
2 NA NA 1 1
3 4 5 5 1
4 3 3 5 1
5 6 6 5 1
6 5 5 1 1
我需要它的长格式,所以转换它:
GapAnalysis_LongForm <- GapAnalysis %>%
gather(key = Product,value = Score, ComSat, ComImpt)
现在有了这个:
status year Product Score
<dbl> <dbl> <chr> <dbl>
1 1. 1. ComSat 2.
2 5. 1. ComSat 5.
3 5. 2. ComSat 3.
4 1. 1. ComSat 5.
5 1. 1. ComImpt 4.
6 5. 1. ComSat 4.
我现在需要将 ComSat 和 ComImpt 重新编码为值(1 和 2),但我很难过。 Recode 和 recode_factor 给了我错误。我正在尝试获得类似这样的输出:
status year Product Score
<dbl> <dbl> <chr> <dbl>
1 1. 1. 1 2.
2 5. 1. 1 5.
3 5. 2. 1 3.
4 1. 1. 1 5.
5 1. 1. 2 4.
6 5. 1. 1 4.
在正确的方向上的任何一般点?
我很感激!!!
【问题讨论】:
-
您参考但从不提供不起作用的代码,也不提供实际的输出/错误;如果您希望获得任何相关帮助,我建议您将这个问题多一点reproducible
-
并删除与问题不直接相关的代码,描述预期输出。
标签: r dplyr tidyr recode key-pair