2017-07-25 40 views
1

代表性的样本数据的列表中提取数据(名单列表):从列表到自己的`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循环 - 将不胜感激。此外,如果有上述资源我想了解,而不是找到正确的代码。

回答

6

可以分三个步骤做到这一点,首先拉出d,然后结合各行的d各个要素,然后再结合一切都变成一个单一的对象。

我使用bind_rowsdplyr作为列表内行绑定。map_df进行最终的行绑定。

library(purrr) 
library(dplyr) 

l %>% 
    map("d") %>% 
    map_df(bind_rows) 

这也相当于:

map_df(l, ~bind_rows(.x[["d"]])) 

结果是这样的:

# A tibble: 12 x 5 
     id   label     link  score externalId 
    <int>   <chr>    <chr>  <dbl>  <dbl> 
1  5   Utah   Asia/Anadyr -0.2110459   NA 
2  8 South Carolina  Pacific/Wallis 0.5265409 -6.743544 
3  9  Nebraska America/Scoresbysund 0.2508955 16.425747 
4  8  Georgia   America/Nome 0.5264941 7.915836 
5  2  Washington  America/Shiprock -0.5551864 15.068666 
6  6 North Dakota   Universal 1.0316830   NA 
7  1 New Hampshire  America/Cordoba 1.2158206 9.727642 
8  1   Alaska  Asia/Istanbul -0.2318326   NA 
9  4 Pennsylvania Africa/Dar_es_Salaam 0.5902453   NA 
10  3  Delaware  Asia/Samarkand 0.6955771 15.236482 
11  4 North Dakota  America/Tortola 1.0306027 -7.216669 
12  9  Nebraska  America/Ojinaga -1.1139800 -8.451451 
0

有关purrr更多的信息,我建议Grolemund韦翰的“R数据科学” http://r4ds.had.co.nz/

我想你所面临的一个问题是,一些在l$d项目是与每一个观测的变量列表,随时可以转换为数据框,而其他项目则是这些列表的列表。

但是我自己并不擅长咕噜噜。这是我会怎么做:

l <- lapply(l, function(x){x$d}) ## work with the data you need. 

list_of_observations <- Filter(function(x) {!is.null(names(x))},l) 

list_of_lists <- Filter(function(x) {is.null(names(x))}, l) 

another_list_of_observations <- unlist(list_of_lists, recursive=FALSE) 

df <- lapply(c(list_of_observations, another_list_of_observations), 
      as.data.frame) %>% bind_rows