【问题标题】:Reshape wide data to long data with all possible combinations使用所有可能的组合将宽数据重塑为长数据
【发布时间】:2020-06-10 19:58:37
【问题描述】:

我正在尝试将宽数据重塑为长格式,我将多列数据放入两列,包括所有可能的组合。

宽数据示例:

> dfWide <- data.frame("id" = 1:5, "code1" = c("a","b", "c", "d", "a"), "code2" = c("b","c", NA, "c", "b"), "code3" = c("c", NA, NA,"b", NA), "code4" = c(NA, NA, NA, "a", NA))
> dfWide
  id code1 code2 code3 code4
1  1     a     b     c  <NA>
2  2     b     c  <NA>  <NA>
3  3     c  <NA>  <NA>  <NA>
4  4     d     c     b     a
5  5     a     b  <NA>  <NA>

下面是所需的长格式。我不担心顺序(即,如果我将“a”与“b”组合,我不需要“b”与“a”。)

> dfLong <- data.frame("id" = c(1, 1, 1, 2, 3, 4, 4, 4, 4, 4, 4, 5), "one" = c("a", "a", "b", "b", "c", "d", "d", "d", "c", "c", "b", "a"), "two" = c("b", "c", "c", "c", NA, "c", "b", "a", "b", "a", "a", "b"))
> dfLong
   id one  two
1   1   a    b
2   1   a    c
3   1   b    c
4   2   b    c
5   3   c <NA>
6   4   d    c
7   4   d    b
8   4   d    a
9   4   c    b
10  4   c    a
11  4   b    a
12  5   a    b

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我们可以转为“长”格式,然后使用group_bycombn

    library(dplyr)
    library(tidyr)
    dfWide %>%
       pivot_longer(cols = -id, values_drop_na = TRUE) %>%
       group_by(id) %>%
       summarise(out = if(n() > 1) combn(as.character(unique(value)), 2,
          FUN = function(x) tibble(one = x[1], two = x[2]),
          simplify = FALSE) else list(tibble(one =value, two = NA_character_))) %>% 
      unnest(c(out))
    # A tibble: 12 x 3
    #      id one   two  
    #   <int> <chr> <chr>
    # 1     1 a     b    
    # 2     1 a     c    
    # 3     1 b     c    
    # 4     2 b     c    
    # 5     3 c     <NA> 
    # 6     4 d     c    
    # 7     4 d     b    
    # 8     4 d     a    
    # 9     4 c     b    
    #10     4 c     a    
    #11     4 b     a    
    #12     5 a     b    
    

    dplyr的版本不同,我们可以让summarise返回单行,然后做双unnest

    dfWide %>%
       pivot_longer(cols = -id, values_drop_na = TRUE) %>%
        group_by(id) %>%
        summarise(out = list(if(n() > 1) combn(as.character(unique(value)), 2,
           FUN = function(x) tibble(one = x[1], two = x[2]),
           simplify = FALSE) else list(tibble(one =value, two = NA_character_)))) %>%
        unnest(c(out)) %>% 
        unnest(c(out))
    # A tibble: 12 x 3
    #     id one   two  
    #   <int> <chr> <chr>
    # 1     1 a     b    
    # 2     1 a     c    
    # 3     1 b     c    
    # 4     2 b     c    
    # 5     3 c     <NA> 
    # 6     4 d     c    
    # 7     4 d     b    
    # 8     4 d     a    
    # 9     4 c     b    
    #10     4 c     a    
    #11     4 b     a    
    #12     5 a     b    
    

    【讨论】:

    • 我收到此错误:Error: Column "out" must be length 1 (a summary value), not 3
    • @Cat 刚刚再次检查了代码。您的示例数据并没有给我带来错误。可能与包版本有关?
    • @Cat 这个错误是基于相同的例子还是不同的例子
    • 该错误基于相同的示例。我重新安装了所有软件包并更新了 r。
    • 我不确定发生了什么。我将不得不再次回到这个问题。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2021-05-01
    • 2021-07-03
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2017-08-08
    相关资源
    最近更新 更多