使用tidyr::pivot_wider时遇「Values are not uniquely identified」警告的解决求助
使用tidyr::pivot_wider时遇「Values are not uniquely identified」警告的解决求助
大家好,我现在在处理一个数据格式转换的问题,遇到了点麻烦,想请教一下各位。
我有一个名为dfmelt的数据框,想要把它从长格式转成宽格式。它包含五列:study_id和redcap_event_name是用来分组的ID列,variable和value是键值对,还有一个数值列num——这个列是按study_id和redcap_event_name分组生成的序列(1、2…n),我希望把这个num作为后缀直接拼在宽格式的列名后面,比如micro_sample_dt1这种形式,不需要分隔符。
以下是我的数据片段:
| study_id | redcap_event_name | variable | value | num |
|---|---|---|---|---|
| CHLA_0014 | csf_shunt_infectio_arm_1 | micro_sample_dt | 7/22/21 | 1 |
| CHLA_0014 | csf_shunt_infectio_arm_1 | micro_sample_dt | 7/22/21 | 2 |
| CHLA_0014 | csf_shunt_infectio_arm_1 | micro_sample_dt | 7/22/21 | 3 |
| CHLA_0014 | csf_shunt_infectio_arm_1 | micro_sample_time | 18:19 | 1 |
| CHLA_0014 | csf_shunt_infectio_arm_1 | micro_sample_time | 18:38 | 2 |
| CHLA_0014 | csf_shunt_infectio_arm_1 | micro_sample_time | 18:38 | 3 |
| CHLA_0067 | initial_shunt_plac_arm_1 | micro_culture_source | 1 | 1 |
| CHLA_0067 | initial_shunt_plac_arm_1 | micro_culture_source | 1 | 2 |
| CHLA_0067 | initial_shunt_plac_arm_1 | micro_culture_source | 1 | 3 |
我尝试用tidyr::pivot_wider来实现,代码如下:
pivot_wider(dfmelt, id_cols = c(1:2), names_from = c(variable), values_from = c(value), names_glue = "{variable}{num}")
但运行后收到了这样的警告:
Warning message:
Values are not uniquely identified; output will contain list-cols.
我想要的目标格式是这样的(缺失值用NA填充就可以):
| study_id | redcap_event_name | micro_sample_dt1 | micro_sample_time1 | micro_culture_source1 | micro_sample_dt2 | micro_sample_time2 | micro_culture_source2 | micro_sample_dt3 | micro_sample_time3 | micro_culture_source3 |
|---|---|---|---|---|---|---|---|---|---|---|
| CHLA_0014 | csf_shunt_infectio_arm_1 | 7/22/21 | 18:19 | NA | 7/22/21 | 18:38 | NA | 7/22/21 | 18:38 | NA |
| CHLA_0067 | initial_shunt_plac_arm_1 | 1 | NA | 1 | 1 | NA | 1 | 1 | NA | 1 |
想请教大家,我该怎么修改代码才能得到预期的宽格式,同时消除这个警告呢?
备注:内容来源于stack exchange,提问作者Nishad Gulvady




