Reputation: 7127
I have some data which looks like:
$`2020-02-14`
# A tibble: 6 × 8
`_id` source target trips source_layer target_layer date updated_at
<chr> <chr> <chr> <dbl> <chr> <chr> <chr> <chr>
1 db92d376f18a2ea569591d92 240 001 20.4 zon_bas_13 zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:47:20 GMT
2 069b35ec5c1cb8559944b047 036 001 6.34 zon_bas_13 zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:47:20 GMT
3 508870263b4d5eb43bff3913 166 001 17.6 zon_bas_13 zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:47:20 GMT
4 f85a22e94e6bf64dbd66b9a8 064 001 23.7 zon_bas_13 zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:47:20 GMT
5 fa9279ea6ed0385e48c1e507 095 001 12.7 zon_bas_13 zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:47:20 GMT
6 a4fcf39d94ac43a942af3ebc 256 001 3.42 zon_bas_13 zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:47:20 GMT
$`2020-02-14`
# A tibble: 6 × 8
`_id` id viajes personas layer date updated_at type
<chr> <chr> <dbl> <dbl> <chr> <chr> <chr> <chr>
1 602fd5cc1bd24545ddba712f 024 0 7310. zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:14:20 GMT zone_movements
2 602fd5cc1bd24545ddba712a 019 0 10043. zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:14:20 GMT zone_movements
3 602fd5cc1bd24545ddba7118 001 0 5670. zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:14:20 GMT zone_movements
4 602fd5cc1bd24545ddba711b 004 0 4055. zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:14:20 GMT zone_movements
5 602fd5cc1bd24545ddba713c 037 0 1728. zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:14:20 GMT zone_movements
6 602fd5cc1bd24545ddba7141 042 0 6941. zon_bas_13 2020-02-14 Fri, 19 Feb 2021 16:14:20 GMT zone_movements
Which are stored in a list. This example is for a single day 2020-02-14
. I have 2 list elements for any given day. I want to store them as two separate lists.
The second list element contains a column type
= zone_movements
. I would like to extract/separate these lists.
My question is, how can I extract and separate these two list elements? The first data frame doesn't contain the type
variable but the second does. I have the same structure for each day in the list.
Data:
data <- list(`2020-02-14` = structure(list(`_id` = c("db92d376f18a2ea569591d92",
"069b35ec5c1cb8559944b047", "508870263b4d5eb43bff3913", "f85a22e94e6bf64dbd66b9a8",
"fa9279ea6ed0385e48c1e507", "a4fcf39d94ac43a942af3ebc"), source = c("240",
"036", "166", "064", "095", "256"), target = c("001", "001",
"001", "001", "001", "001"), trips = c(20.3755659815376, 6.33660060507582,
17.5549564408883, 23.6903524660744, 12.7358490501818, 3.42061069389371
), source_layer = c("zon_bas_13", "zon_bas_13", "zon_bas_13",
"zon_bas_13", "zon_bas_13", "zon_bas_13"), target_layer = c("zon_bas_13",
"zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13"
), date = c("2020-02-14", "2020-02-14", "2020-02-14", "2020-02-14",
"2020-02-14", "2020-02-14"), updated_at = c("Fri, 19 Feb 2021 16:47:20 GMT",
"Fri, 19 Feb 2021 16:47:20 GMT", "Fri, 19 Feb 2021 16:47:20 GMT",
"Fri, 19 Feb 2021 16:47:20 GMT", "Fri, 19 Feb 2021 16:47:20 GMT",
"Fri, 19 Feb 2021 16:47:20 GMT")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame")), `2020-02-14` = structure(list(`_id` = c("602fd5cc1bd24545ddba712f",
"602fd5cc1bd24545ddba712a", "602fd5cc1bd24545ddba7118", "602fd5cc1bd24545ddba711b",
"602fd5cc1bd24545ddba713c", "602fd5cc1bd24545ddba7141"), id = c("024",
"019", "001", "004", "037", "042"), viajes = c(0, 0, 0, 0, 0,
0), personas = c(7309.52639452941, 10042.6791128304, 5669.81907958993,
4054.73963049056, 1728.46084911973, 6941.14136612122), layer = c("zon_bas_13",
"zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13"
), date = c("2020-02-14", "2020-02-14", "2020-02-14", "2020-02-14",
"2020-02-14", "2020-02-14"), updated_at = c("Fri, 19 Feb 2021 16:14:20 GMT",
"Fri, 19 Feb 2021 16:14:20 GMT", "Fri, 19 Feb 2021 16:14:20 GMT",
"Fri, 19 Feb 2021 16:14:20 GMT", "Fri, 19 Feb 2021 16:14:20 GMT",
"Fri, 19 Feb 2021 16:14:20 GMT"), type = c("zone_movements",
"zone_movements", "zone_movements", "zone_movements", "zone_movements",
"zone_movements")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame")), `2020-02-15` = structure(list(`_id` = c("666d8a26e56c1de3c6377c9c",
"5e8cf5f431e1438e8782e45a", "bd9b323d2b81e5c27e5dadaf", "58ee76ed2659d5282fa758fd",
"1c30712288a521b433c1f2d1", "ed253b9534efd319e6656a6f"), source = c("241",
"240", "036", "166", "064", "095"), target = c("001", "001",
"001", "001", "001", "001"), trips = c(23.4932227649895, 16.4087522615295,
14.064685158181, 16.6838492808322, 14.7524719921587, 9.08024431531525
), source_layer = c("zon_bas_13", "zon_bas_13", "zon_bas_13",
"zon_bas_13", "zon_bas_13", "zon_bas_13"), target_layer = c("zon_bas_13",
"zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13"
), date = c("2020-02-15", "2020-02-15", "2020-02-15", "2020-02-15",
"2020-02-15", "2020-02-15"), updated_at = c("Fri, 19 Feb 2021 17:04:53 GMT",
"Fri, 19 Feb 2021 17:04:53 GMT", "Fri, 19 Feb 2021 17:04:53 GMT",
"Fri, 19 Feb 2021 17:04:53 GMT", "Fri, 19 Feb 2021 17:04:53 GMT",
"Fri, 19 Feb 2021 17:04:53 GMT")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame")), `2020-02-15` = structure(list(`_id` = c("602fd5cd1bd24545ddba8383",
"602fd5cd1bd24545ddba8385", "602fd5cd1bd24545ddba838b", "602fd5cd1bd24545ddba8381",
"602fd5cd1bd24545ddba839f", "602fd5cd1bd24545ddba8368"), id = c("035",
"037", "043", "033", "063", "008"), viajes = c(0, 0, 0, 0, 0,
0), personas = c(6163.04408054466, 2380.89478299232, 5009.73958221377,
3835.06265049629, 4532.7223999508, 5581.86113064816), layer = c("zon_bas_13",
"zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13", "zon_bas_13"
), date = c("2020-02-15", "2020-02-15", "2020-02-15", "2020-02-15",
"2020-02-15", "2020-02-15"), updated_at = c("Fri, 19 Feb 2021 16:14:21 GMT",
"Fri, 19 Feb 2021 16:14:21 GMT", "Fri, 19 Feb 2021 16:14:21 GMT",
"Fri, 19 Feb 2021 16:14:21 GMT", "Fri, 19 Feb 2021 16:14:21 GMT",
"Fri, 19 Feb 2021 16:14:21 GMT"), type = c("zone_movements",
"zone_movements", "zone_movements", "zone_movements", "zone_movements",
"zone_movements")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame")))
Upvotes: 0
Views: 35
Reputation: 887531
IF we want to create two lists,
lst1 <- data[c(TRUE, FALSE)]
lst2 <- data[c(FALSE, TRUE)]
Upvotes: 1