Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active December 3, 2018 06:43
Show Gist options
  • Save brshallo/35d3f36e5099504609425b105819890a to your computer and use it in GitHub Desktop.
Save brshallo/35d3f36e5099504609425b105819890a to your computer and use it in GitHub Desktop.
List-to-df-with-index-maintained-question
# unnest list, mantain index example
library(tidyverse, quietly = TRUE)
(list_test <- list(1, 1, 1, 1, c(1, 2), 3, 3, c(2, 4)))
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 1
#>
#> [[3]]
#> [1] 1
#>
#> [[4]]
#> [1] 1
#>
#> [[5]]
#> [1] 1 2
#>
#> [[6]]
#> [1] 3
#>
#> [[7]]
#> [1] 3
#>
#> [[8]]
#> [1] 2 4
(tibble(nc_buildings = list_test) %>%
mutate(row_n = row_number()) %>%
unnest())
#> # A tibble: 10 x 2
#> row_n nc_buildings
#> <int> <dbl>
#> 1 1 1
#> 2 2 1
#> 3 3 1
#> 4 4 1
#> 5 5 1
#> 6 5 2
#> 7 6 3
#> 8 7 3
#> 9 8 2
#> 10 8 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment