[英]How to convert this list of lists of lists into data frame in R?
假設我們有這樣的東西:
list of two months:
Jan
Feb
然后,當您展開一月和二月時,您會看到其他列表(國家/地區):
Jan -> USA, UK, etc.
Fab -> USA, UK, etc.
最后,每個國家都是一個列表,包含兩個項目。 例如在 Jan 列表中打開 USA:
USA -> gdp, population
我想要做的是實現這樣的數據框(數字只是數據中的值):
month country gdp population
Jan USA number no.
Jan UK number no.
Feb USA number no.
Feb UK number no.
我嘗試bind_rows(list)
,但效果不太好,因為 header 成為國家,並且列中填充了不再有標簽的數字。
有沒有人有任何想法?
編輯: dput()
output:
list(`Jan` = list(UK = list(date_value = "Jan",
country_code = "UK", gdp = 308L, pop = 0L)
這是一個選項
library(purrr)
library(dplyr)
map_dfr(lst2, bind_rows)
# A tibble: 2 x 4
# date_value country_code gdp pop
# <chr> <chr> <int> <int>
#1 Jan UK 308 0
#2 Jan UK 308 0
lst2 <- list(Jan = list(UK = list(date_value = "Jan", country_code = "UK",
gdp = 308L, pop = 0L)), Feb = list(UK = list(date_value = "Jan",
country_code = "UK", gdp = 308L, pop = 0L)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.