2017-11-11 4 views
7

REST 호출에서받은 중첩 목록이 있습니다. 응답에는 기본 관계형 데이터베이스의 중첩 된 목록이 포함됩니다. 나는 분석을 단순화하기 위해리스트를 평평하게하고 싶다. 나는 purrr tutorial에있는 가이드 라인을 따르려고 노력했으나 제대로 작동하지 않습니다.tidyverse 도구를 사용하여 관계형 데이터베이스에서 파생 된 중첩 목록을 병합하는 가장 좋은 방법은 무엇입니까?

내가 평평 출력을 찾고

hist1 <- list(field="type", from_string ="issue", to_string="bug") 
hist2 <- list(field="status", from_string ="open", to_string="closed") 
hist3 <- list(field="type", from_string ="bug", to_string="issue") 
issue1 <- list(id="123", created = "2017-11-08", issue_history = list(hist1, hist2)) 
issue2 <- list(id="124", created = "2017-11-10", issue_history = list(hist1, hist3)) 
issue <- list(issue1, issue2) 

내 간단한 입력 : 이것에 대한 scable 논리를 구축하는 가장 좋은 방법입니다

id created type from_string to_string 
123 2017-11-08 type issue  bug 
123 2017-11-08 status open   closed 
123 2017-11-10 type bug   issue 

?

(나를 위해) 베스트 : 간단

  • 코드 문제, 즉 성능과 메모리가 중요한 요소하지 않은 수백만 확장 할 필요가 없습니다
  • 을 유지하기 tidyverse에서

    • 도구
  • +0

    많은 감사가 나를 유지하기 위해 덜 복잡 할 것 – rgustavs

    답변

    6

    네이트의 대답 @에서 영감을 또 다른 해결책 : 도움 @Psidom 솔루션

    map_df(issue, as_tibble) %>% 
        mutate(issue_history = map(issue_history, as_tibble)) %>% 
        unnest() 
    
    # A tibble: 4 x 5 
    #  id created field from_string to_string 
    # <chr>  <chr> <chr>  <chr>  <chr> 
    #1 123 2017-11-08 type  issue  bug 
    #2 123 2017-11-08 status  open closed 
    #3 124 2017-11-10 type  issue  bug 
    #4 124 2017-11-10 type   bug  issue 
    
    5

    더 많은 정보가 있으면 purrr 잘 모르지만 작동합니다.

    library(tidyverse) 
    
    map(issue, as.tibble) %>% 
        map_df(~ rowwise(.) %>% 
          mutate(issue_history = list(bind_rows(issue_history))) %>% 
          unnest()) 
    
    # A tibble: 4 x 5 
        id created field from_string to_string 
        <chr>  <chr> <chr>  <chr>  <chr> 
    1 123 2017-11-08 type  issue  bug 
    2 123 2017-11-08 status  open closed 
    3 124 2017-11-10 type  issue  bug 
    4 124 2017-11-10 type   bug  issue 
    
    관련 문제