melt_list

melt_list(list, ..., na.rm = TRUE)

melt_tree(x, names, ...)

Arguments

list

A list object, with the same colnames data.frame in every element.

...

other parameters to melt

na.rm

Boolean

var.name

vector of id variables. Can be integer (variable position) or string (variable name). If blank, will use all non-measured variables.

Examples

# data.frame
df <- data.frame(year = 2010, day = 1:3, month = 1, site = "A")
l <- list(a = df, b = df)

melt_list(l, 'id') %>% as_tibble()
#> # A tibble: 6 × 5
#>   id     year   day month site 
#>   <fct> <dbl> <int> <dbl> <chr>
#> 1 a      2010     1     1 A    
#> 2 a      2010     2     1 A    
#> 3 a      2010     3     1 A    
#> 4 b      2010     1     1 A    
#> 5 b      2010     2     1 A    
#> 6 b      2010     3     1 A    
melt_list(l, 'id' = 1) %>% as_tibble()
#> # A tibble: 6 × 5
#>      id  year   day month site 
#>   <dbl> <dbl> <int> <dbl> <chr>
#> 1     1  2010     1     1 A    
#> 2     1  2010     2     1 A    
#> 3     1  2010     3     1 A    
#> 4     1  2010     1     1 A    
#> 5     1  2010     2     1 A    
#> 6     1  2010     3     1 A    
melt_list(set_names(l, NULL), 'id') %>% as_tibble()
#> # A tibble: 6 × 5
#>      id  year   day month site 
#>   <int> <dbl> <int> <dbl> <chr>
#> 1     1  2010     1     1 A    
#> 2     1  2010     2     1 A    
#> 3     1  2010     3     1 A    
#> 4     2  2010     1     1 A    
#> 5     2  2010     2     1 A    
#> 6     2  2010     3     1 A    

# data.table
df <- data.table::data.table(year = 2010, day = 1:3, month = 1, site = "A")
l <- list(a = df, b = df)
melt_list(l, "id")
#> [data.table]: 
#> # A data frame: 6 × 5
#>   id     year   day month site 
#>   <fct> <dbl> <int> <dbl> <chr>
#> 1 a      2010     1     1 A    
#> 2 a      2010     2     1 A    
#> 3 a      2010     3     1 A    
#> 4 b      2010     1     1 A    
#> 5 b      2010     2     1 A    
#> 6 b      2010     3     1 A    
melt_list(l, id = c("a", "b"))
#> [data.table]: 
#> # A data frame: 6 × 5
#>   id     year   day month site 
#>   <fct> <dbl> <int> <dbl> <chr>
#> 1 a      2010     1     1 A    
#> 2 a      2010     2     1 A    
#> 3 a      2010     3     1 A    
#> 4 b      2010     1     1 A    
#> 5 b      2010     2     1 A    
#> 6 b      2010     3     1 A    
# int `id` is much more efficient
melt_list(l, id = c(1, 2))
#> [data.table]: 
#> # A data frame: 6 × 5
#>      id  year   day month site 
#>   <dbl> <dbl> <int> <dbl> <chr>
#> 1     1  2010     1     1 A    
#> 2     1  2010     2     1 A    
#> 3     1  2010     3     1 A    
#> 4     2  2010     1     1 A    
#> 5     2  2010     2     1 A    
#> 6     2  2010     3     1 A