【发布时间】:2021-04-12 06:36:53
【问题描述】:
我有 2 个如下所示的 R 数据框:
数据帧 1:
| identifier | ef_posterior | position_no | classification |
|---|---|---|---|
| 11111 | 0.260 | 1 | yes |
| 11111 | 0.0822 | 2 | yes |
| 11111 | 0.00797 | 3 | yes |
| 11111 | 0.04 | 4 | no |
| 11111 | 0.245 | 5 | yes |
| 11111 | 0.432 | 6 | yes |
| 11112 | 0.342 | 1 | maybe |
| 11112 | 0.453 | 2 | yes |
| 11112 | 0.0032 | 3 | yes |
| 11112 | 0.241 | 5 | no |
| 11112 | 0.0422 | 6 | yes |
| 11112 | 0.311 | 4 | no |
数据框 2:
| study_identifier | %LVEF |
|---|---|
| 11111 | 62 |
| 11112 | 76 |
我想将这两个数据框合并并重新排列成这样的:
Study_identifier 和 identifier 是同一个东西(只是不同的列名)。另外,我想重新编码分类,使yes = 0,no = 1,maybe = 2
| identifier | pos_1 | pos_1_class | pos_2 | pos_2_class | pos_3 | pos_3_class | pos_4 | pos_4_class | pos_5 | pos_5_class | pos_6 | pos_6_class | %LVEF |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 11111 | 0.260 | 0 | 0.0822 | 0 | 0.00797 | 0 | 0.04 | 1 | 0.245 | 0 | 0.432 | 0 | 62 |
| 11112 | 0.342 | 2 | 0.453 | 0 | 0.0032 | 0 | 0.311 | 1 | 0.241 | 1 | 0.0422 | 0 | 76 |
df1 %>% mutate(position_no = paste0("position_", position_no)) %>%
pivot_wider(id_cols = identifier, names_from = position_no, values_from = ef_posterior) %>%
left_join(df2 %>% mutate(study_identifier = as.numeric(as.character(study_identifier))), by = c("identifier" = "study_identifier"))
这是我现在的代码,但我不知道在哪里放置分类列的代码
我该怎么做呢? 任何帮助将不胜感激!
【问题讨论】: