【发布时间】:2020-05-13 15:53:07
【问题描述】:
我有以下数据集格式:
structure(list(ï..Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
2L), .Label = c("1/1/2019", "1/2/2019"), class = "factor"), ID = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L), .Label = c("AAA001", "BBB002"), class = "factor"),
Gender = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("FEMALE",
"MALE"), class = "factor"), Measure = structure(c(1L, 2L,
4L, 3L, 3L, 3L, 4L), .Label = c("Mental Status", "Motor Function",
"No. 1", "Score"), class = "factor"), Value = structure(c(6L,
6L, 5L, 1L, 2L, 3L, 4L), .Label = c("1.", "2/2/2020", "3811: Satisfactory",
"4", "7", "Normal"), class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
输出:
df
ï..Date ID Gender Measure Value
1 1/1/2019 AAA001 MALE Mental Status Normal
2 1/1/2019 AAA001 MALE Motor Function Normal
3 1/1/2019 AAA001 MALE Score 7
4 1/1/2019 BBB002 FEMALE No. 1 1.
5 1/1/2019 BBB002 FEMALE No. 1 2/2/2020
6 1/1/2019 BBB002 FEMALE No. 1 3811: Satisfactory
7 1/2/2019 BBB002 FEMALE Score 4
>
我尝试了以下但遇到了错误,其中出现了近 7000 个重复,主要是由于同一 ID 的Measures 的重复行:
df %>%
+ distinct() %>%
+ pivot_wider(names_from = 'Measure', values_from = 'Value')
# A tibble: 3 x 7
ï..Date ID Gender `Mental Status` `Motor Function` Score `No. 1`
<fct> <fct> <fct> <list<fct>> <list<fct>> <list<fct>> <list<fct>>
1 1/1/2019 AAA001 MALE [1] [1] [1] [0]
2 1/1/2019 BBB002 FEMALE [0] [0] [0] [3]
3 1/2/2019 BBB002 FEMALE [0] [0] [1] [0]
Warning message:
Values in `Value` are not uniquely identified; output will contain list-cols.
* Use `values_fn = list(Value = list)` to suppress this warning.
* Use `values_fn = list(Value = length)` to identify where the duplicates arise
* Use `values_fn = list(Value = summary_fun)` to summarise duplicates
预期的输出应如下所示:
Date ID Gender Mental.Status Motor.Function No.1 Score
1 1/1/2019 AAA001 MALE Normal Normal 7
2 1/1/2019 BBB002 FEMALE 1. 2/2/2020, 3811: Satisfactory 4
提前感谢您的帮助!
【问题讨论】:
标签: r