2016-12-04 58 views
1

我试图拉平深深地/不规则嵌套列表/ JSON对象到数据帧中R.如何扁平化深深和不规则嵌套列表/ JSON R中

键名称是一致的,但数量嵌套元素的不同从一个元素到下一个元素。

我试过使用jsonlitetidyr::unnest函数展平列表,但tidyr::unnest不能展开包含多个新列的列表列。我也尝试使用purrr包中的map函数,但无法获取任何内容。

JSON数据的子集位于下方,列表对象包含在本文末尾。

[ 
    { 
    "name": ["Hillary Clinton"], 
    "type": ["PERSON"], 
    "metadata": { 
     "mid": ["/m/0d06m5"], 
     "wikipedia_url": ["http://en.wikipedia.org/wiki/Hillary_Clinton"] 
    }, 
    "salience": [0.2883], 
    "mentions": [ 
     { 
     "text": { 
      "content": ["Clinton"], 
      "beginOffset": [132] 
     }, 
     "type": ["PROPER"] 
     }, 
     { 
     "text": { 
      "content": ["Mrs."], 
      "beginOffset": [127] 
     }, 
     "type": ["COMMON"] 
     }, 
     { 
     "text": { 
      "content": ["Clinton"], 
      "beginOffset": [403] 
     }, 
     "type": ["PROPER"] 
     }, 
     { 
     "text": { 
      "content": ["Mrs."], 
      "beginOffset": [398] 
     }, 
     "type": ["COMMON"] 
     }, 
     { 
     "text": { 
      "content": ["Hillary Clinton"], 
      "beginOffset": [430] 
     }, 
     "type": ["PROPER"] 
     } 
    ] 
    }, 
    { 
    "name": ["Trump"], 
    "type": ["PERSON"], 
    "metadata": { 
     "mid": ["/m/0cqt90"], 
     "wikipedia_url": ["http://en.wikipedia.org/wiki/Donald_Trump"] 
    }, 
    "salience": [0.245], 
    "mentions": [ 
     { 
     "text": { 
      "content": ["Trump"], 
      "beginOffset": [24] 
     }, 
     "type": ["PROPER"] 
     }, 
     { 
     "text": { 
      "content": ["Mr."], 
      "beginOffset": [20] 
     }, 
     "type": ["COMMON"] 
     } 
    ] 
    } 
] 

而期望的输出将是一个像下面的数据框,其中外部元素重复,每个最内部的元素都有自己的行。

name    type  metadata.mid metadata.wikipedia_url       salience mentions.text.content mentions.text.beginOffset mentions.type 
Hillary Clinton  PERSON  /m/0d06m5  http://en.wikipedia.org/wiki/Hillary_Clinton 0.2883  Clinton     132       PROPER 
Hillary Clinton  PERSON  /m/0d06m5  http://en.wikipedia.org/wiki/Hillary_Clinton 0.2883  Mrs.     127       COMMON 
Hillary Clinton  PERSON  /m/0d06m5  http://en.wikipedia.org/wiki/Hillary_Clinton 0.2883  Clinton     403       PROPER 
Hillary Clinton  PERSON  /m/0d06m5  http://en.wikipedia.org/wiki/Hillary_Clinton 0.2883  Mrs.     398       COMMON 
Hillary Clinton  PERSON  /m/0d06m5  http://en.wikipedia.org/wiki/Hillary_Clinton 0.2883  Hillary Clinton   430       PROPER 
Trump    PERSON  /m/0cqt90  http://en.wikipedia.org/wiki/Donald_Trump  0.245  Trump     24       PROPER 
Trump    PERSON  /m/0cqt90  http://en.wikipedia.org/wiki/Donald_Trump  0.245  Mr.      20       COMMON 

是否有一种通用/可扩展的方法来平滑这种类型的数据?


的R列表对象:

nested_list <- list(structure(list(name = "Hillary Clinton", type = "PERSON", 
    metadata = structure(list(mid = "/m/0d06m5", wikipedia_url = "http://en.wikipedia.org/wiki/Hillary_Clinton"), .Names = c("mid", 
    "wikipedia_url")), salience = 0.28831193, mentions = list(
     structure(list(text = structure(list(content = "Clinton", 
      beginOffset = 132L), .Names = c("content", "beginOffset" 
     )), type = "PROPER"), .Names = c("text", "type")), structure(list(
      text = structure(list(content = "Mrs.", beginOffset = 127L), .Names = c("content", 
      "beginOffset")), type = "COMMON"), .Names = c("text", 
     "type")), structure(list(text = structure(list(content = "Clinton", 
      beginOffset = 403L), .Names = c("content", "beginOffset" 
     )), type = "PROPER"), .Names = c("text", "type")), structure(list(
      text = structure(list(content = "Mrs.", beginOffset = 398L), .Names = c("content", 
      "beginOffset")), type = "COMMON"), .Names = c("text", 
     "type")), structure(list(text = structure(list(content = "Hillary Clinton", 
      beginOffset = 430L), .Names = c("content", "beginOffset" 
     )), type = "PROPER"), .Names = c("text", "type")))), .Names = c("name", 
"type", "metadata", "salience", "mentions")), structure(list(
    name = "Trump", type = "PERSON", metadata = structure(list(
     mid = "/m/0cqt90", wikipedia_url = "http://en.wikipedia.org/wiki/Donald_Trump"), .Names = c("mid", 
    "wikipedia_url")), salience = 0.24501903, mentions = list(
     structure(list(text = structure(list(content = "Trump", 
      beginOffset = 24L), .Names = c("content", "beginOffset" 
     )), type = "PROPER"), .Names = c("text", "type")), structure(list(
      text = structure(list(content = "Mr.", beginOffset = 20L), .Names = c("content", 
      "beginOffset")), type = "COMMON"), .Names = c("text", 
     "type")))), .Names = c("name", "type", "metadata", "salience", 
"mentions"))) 

回答

3

一种方法:

map_df(nested_list, function(x) { 

    df <- flatten_df(x[c("name", "type", "metadata", "salience")]) 

    map_df(x$mentions, ~c(as.list(.$text), mentions_type=.$type)) %>% 
    mutate(name=df$name, type=df$type, mid=df$mid, 
      wikipedia_url=df$wikipedia_url, salience=df$salience) 

}) %>% glimpse() 
## Observations: 7 
## Variables: 8 
## $ content  <chr> "Clinton", "Mrs.", "Clinton", "Mrs.", "Hillary Clinton", "Trump", "Mr." 
## $ beginOffset <int> 132, 127, 403, 398, 430, 24, 20 
## $ mentions_type <chr> "PROPER", "COMMON", "PROPER", "COMMON", "PROPER", "PROPER", "COMMON" 
## $ name   <chr> "Hillary Clinton", "Hillary Clinton", "Hillary Clinton", "Hillary Clinton", "Hillary Clinton", "Trump", "Trump" 
## $ type   <chr> "PERSON", "PERSON", "PERSON", "PERSON", "PERSON", "PERSON", "PERSON" 
## $ mid   <chr> "/m/0d06m5", "/m/0d06m5", "/m/0d06m5", "/m/0d06m5", "/m/0d06m5", "/m/0cqt90", "/m/0cqt90" 
## $ wikipedia_url <chr> "http://en.wikipedia.org/wiki/Hillary_Clinton", "http://en.wikipedia.org/wiki/Hillary_Clinton", "http://en.wikipedia.org/wiki/Hillary_Clinton", "http://en.wikiped... 
## $ salience  <dbl> 0.2883119, 0.2883119, 0.2883119, 0.2883119, 0.2883119, 0.2450190, 0.2450190 
+0

谢谢!要检索'mentions> type'字段,我会做一些类似于map_df(x $ mentions,...'line?或者,您是否有足够的资源来理解如何使用'purrr :: map * '函数? – Brian

+1

Aye。答案已更新,以添加该字段。我在1月份在rstudioconf上对'purrr'(更一般地说,管道)进行了演示,所以我会尝试在此处添加链接,但是您可以观察在他们的网站或http://rud.is/b在一月之后。 – hrbrmstr