【问题标题】:Merging Duplicate Rows and apply Pivot_wider合并重复行并应用 Pivot_wider
【发布时间】: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


    【解决方案1】:

    一种方法是将summarizepaste 的值一起使用:

    library(dplyr)
    library(tidyr)
    df %>% 
      group_by(`ï..Date`,`ID`,`Measure`) %>%
      summarize(Value = paste(Value, collapse = ", ")) %>% 
      ungroup %>%
      pivot_wider(names_from = 'Measure', values_from = 'Value')
    ## A tibble: 3 x 6
    #  ï..Date  ID     `Mental Status` `Motor Function` Score `No. 1`                         
    #  <fct>    <fct>  <chr>           <chr>            <chr> <chr>                           
    #1 1/1/2019 AAA001 Normal          Normal           7     NA                              
    #2 1/1/2019 BBB002 NA              NA               NA    1., 2/2/2020, 3811: Satisfactory
    #3 1/2/2019 BBB002 NA              NA               4     NA                        
    

    【讨论】:

    • Btw Ian,结果应该如何输出到另一个 df,比如说 df1?
    • 把第一行改成df1 &lt;- df %&gt;%
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 2012-07-15
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多