tidyr: multiple unnesting with varying NA counts

前端 未结 2 727
孤城傲影
孤城傲影 2020-12-18 09:40

I\'m confused about some tidyr behavior. I can unnest a single response like this:

library(tidyr)

resp1 <- c(\"A\", \"B; A\", \"B\", NA, \"B\")
resp2 <         


        
相关标签:
2条回答
  • 2020-12-18 10:16

    In addition to Psidom answer: by default, unnest drops additional list columns (if row duplication is required).

    Use .drop = FALSE argument to keep other columns.

    Line unnest(resp1) %>% unnest(resp2) %>% unnest(resp3) becomes:

    unnest(resp1, .drop = FALSE) %>% unnest(resp2, .drop = FALSE) %>% unnest(resp3)
    
    0 讨论(0)
  • 2020-12-18 10:30

    Check this link, which shows a different situation of unnesting multiple columns from yours. According to the documentation and the link given, unless there is some clever way to do this, the function might be just defined for a single column to avoid the ambiguity.

    So you may have to unnest your columns one by one, and the code given below might be still cumbersome but simplifies a little bit.

    > resp1 <- c("A", "B; A", "B", NA, "B")
    > resp2 <- c("C; D; F", NA, "C; F", "D", "E")
    > resp3 <- c(NA, NA, "G; H; I", "H; I", "I")
    > data <- data.frame(resp1, resp2, resp3, stringsAsFactors = F)
    > data
      resp1   resp2   resp3
    1     A C; D; F    <NA>
    2  B; A    <NA>    <NA>
    3     B    C; F G; H; I
    4  <NA>       D    H; I
    5     B       E       I
    library(tidyr)
    library(dplyr)
    data %>%
    transform(resp1 = strsplit(resp1, "; "),
              resp2 = strsplit(resp2, "; "),
              resp3 = strsplit(resp3, "; ")) %>%
    unnest(resp1) %>% unnest(resp2) %>% unnest(resp3)
       resp1 resp2 resp3
    1      A     C  <NA>
    2      A     D  <NA>
    3      A     F  <NA>
    4      B  <NA>  <NA>
    5      A  <NA>  <NA>
    6      B     C     G
    7      B     C     H
    8      B     C     I
    9      B     F     G
    10     B     F     H
    11     B     F     I
    12  <NA>     D     H
    13  <NA>     D     I
    14     B     E     I
    
    0 讨论(0)
提交回复
热议问题