Utility functions for data type conversion between data.frame and list.
Source:R/data_preparation.R
data_preparation.Rd
Utility functions for data type conversion between data.frame and list.
Examples
# Convert data.frame to list
d <- dplyr::tibble(name = 1:6,
A = c(rep(TRUE, 5), FALSE),
B = rep(c(FALSE, TRUE), each = 3))
print(d)
#> # A tibble: 6 × 3
#> name A B
#> <int> <lgl> <lgl>
#> 1 1 TRUE FALSE
#> 2 2 TRUE FALSE
#> 3 3 TRUE FALSE
#> 4 4 TRUE TRUE
#> 5 5 TRUE TRUE
#> 6 6 FALSE TRUE
data_frame_to_list(d)
#> $A
#> [1] 1 2 3 4 5
#>
#> $B
#> [1] 4 5 6
#>
# Convert list to data.frame
a <- list(A = 1:5, B = 4:6)
print(a)
#> $A
#> [1] 1 2 3 4 5
#>
#> $B
#> [1] 4 5 6
#>
list_to_data_frame(a)
#> # A tibble: 6 × 3
#> `_key` A B
#> <int> <lgl> <lgl>
#> 1 1 TRUE FALSE
#> 2 2 TRUE FALSE
#> 3 3 TRUE FALSE
#> 4 4 TRUE TRUE
#> 5 5 TRUE TRUE
#> 6 6 FALSE TRUE
# Round-trip conversion
identical(a, data_frame_to_list(list_to_data_frame(a))) # TRUE
#> [1] TRUE
identical(d, list_to_data_frame(data_frame_to_list(d))) # TRUE
#> [1] FALSE