代表性的樣本數據的列表中提取數據(名單列表):從列表到自己的`data.frame`與`purrr`
l <- list(structure(list(a = -1.54676469632688, b = "s", c = "T",
d = structure(list(id = 5L, label = "Utah", link = "Asia/Anadyr",
score = -0.21104594634643), .Names = c("id", "label",
"link", "score")), e = 49.1279871269422), .Names = c("a",
"b", "c", "d", "e")), structure(list(a = -0.934821052832427,
b = "k", c = "T", d = list(structure(list(id = 8L, label = "South Carolina",
link = "Pacific/Wallis", score = 0.526540892113734, externalId = -6.74354377676955), .Names = c("id",
"label", "link", "score", "externalId")), structure(list(
id = 9L, label = "Nebraska", link = "America/Scoresbysund",
score = 0.250895465294041, externalId = 16.4257470807879), .Names = c("id",
"label", "link", "score", "externalId"))), e = 52.3161400117052), .Names = c("a",
"b", "c", "d", "e")), structure(list(a = -0.27261485993069, b = "f",
c = "P", d = list(structure(list(id = 8L, label = "Georgia",
link = "America/Nome", score = 0.526494135483816, externalId = 7.91583574935589), .Names = c("id",
"label", "link", "score", "externalId")), structure(list(
id = 2L, label = "Washington", link = "America/Shiprock",
score = -0.555186440792989, externalId = 15.0686663219837), .Names = c("id",
"label", "link", "score", "externalId")), structure(list(
id = 6L, label = "North Dakota", link = "Universal",
score = 1.03168296038975), .Names = c("id", "label",
"link", "score")), structure(list(id = 1L, label = "New Hampshire",
link = "America/Cordoba", score = 1.21582056168681, externalId = 9.7276418869132), .Names = c("id",
"label", "link", "score", "externalId")), structure(list(
id = 1L, label = "Alaska", link = "Asia/Istanbul", score = -0.23183264861979), .Names = c("id",
"label", "link", "score")), structure(list(id = 4L, label = "Pennsylvania",
link = "Africa/Dar_es_Salaam", score = 0.590245339334121), .Names = c("id",
"label", "link", "score"))), e = 132.1153538536), .Names = c("a",
"b", "c", "d", "e")), structure(list(a = 0.202685974077313, b = "x",
c = "O", d = structure(list(id = 3L, label = "Delaware",
link = "Asia/Samarkand", score = 0.695577130634724, externalId = 15.2364820698193), .Names = c("id",
"label", "link", "score", "externalId")), e = 97.9908914452971), .Names = c("a",
"b", "c", "d", "e")), structure(list(a = -0.396243444741009,
b = "z", c = "P", d = list(structure(list(id = 4L, label = "North Dakota",
link = "America/Tortola", score = 1.03060272795705, externalId = -7.21666936522344), .Names = c("id",
"label", "link", "score", "externalId")), structure(list(
id = 9L, label = "Nebraska", link = "America/Ojinaga",
score = -1.11397997280413, externalId = -8.45145052697411), .Names = c("id",
"label", "link", "score", "externalId"))), e = 123.597945533926), .Names = c("a",
"b", "c", "d", "e")))
我有一個列表的列表,憑藉JSON的數據下載。
該列表有176個元素,每個元素有33個嵌套元素,其中一些元素也是不同長度的列表。
我有興趣分析包含在特定嵌套列表中的數據,每個176有4個或5個元素,其中一些有4個,有些有5個。提取這個嵌套的興趣列表並將其轉換爲data.frame
以便能夠執行一些分析。
在上面的代表性示例數據中,我對l
的5個元素中的每個元素的嵌套列表d
感興趣。因此,期望data.frame
看起來是這樣的:
id label link score externalId
5 Utah Asia/Anadyr -0.2110459 NA
8 South Carolina Pacific/Wallis 0.5265409 -6.743544
.
.
我一直在嘗試使用purrr
這似乎對列表中的處理數據的合理和穩定的流量,但我遇到了錯誤,我不能完全瞭解原因 - 很可能是因爲我沒有正確理解purrr
或列表(可能兩者)的命令/邏輯。這是我已經嘗試的代碼,但將引發相關的誤差:
df <- map_df(l, "d", ~as.data.frame(.))
Error: incompatible sizes (5 != 4)
相信這具有的d
每個組件的不同的長度,或者做不同的包含的數據(有時4個元素有時5 )或者我在這裏使用的函數是錯誤指定的 - 實際上我不完全確定。
我已經通過使用for循環來解決這個問題,我知道這是低效的,因此我的問題在這裏。
這是for循環我目前使用:
df <- data.frame(id = integer(), label = character(), score = numeric(), externalId = numeric())
for(i in seq_along(l)){
df_temp <- l[[i]][[4]] %>% map_df(~as.data.frame(.))
df <- rbind(df, df_temp)
}
一些援助最好用purrr
- 或者一些版本的apply
,因爲這仍然是優於我的for循環 - 將不勝感激。此外,如果有上述資源我想了解,而不是找到正確的代碼。