如何从具有不同数量的变量的嵌套列表创建data.frame

时间:2014-10-16 06:11:34

标签: r list nested-lists

我已经下载了诺贝尔奖获得者的json文件,并将其转换为名为“nobels'”的列表。结构中显示了几条记录

str(nobels)  

List of 1
$ laureates:List of 2
  ..$ :List of 13
  .. ..$ id             : chr "359"
  .. ..$ firstname      : chr "Axel Hugo Theodor"
  .. ..$ surname        : chr "Theorell"
  .. ..$ born           : chr "1903-07-06"
  .. ..$ died           : chr "1982-08-15"
  .. ..$ bornCountry    : chr "Sweden"
  .. ..$ bornCountryCode: chr "SE"
  .. ..$ bornCity       : chr "Linköping"
  .. ..$ diedCountry    : chr "Sweden"
  .. ..$ diedCountryCode: chr "SE"
  .. ..$ diedCity       : chr "Stockholm"
  .. ..$ gender         : chr "male"
  .. ..$ prizes         :List of 1
  .. .. ..$ :List of 5
  .. .. .. ..$ year        : chr "1955"
  .. .. .. ..$ category    : chr "medicine"
  .. .. .. ..$ share       : chr "1"
  .. .. .. ..$ motivation  : chr "\"for his discoveries concerning the nature and mode of action of oxidation enzymes\""
  .. .. .. ..$ affiliations:List of 1
  .. .. .. .. ..$ :List of 3
  .. .. .. .. .. ..$ name   : chr "Karolinska Institutet, Nobel Medical Institute"
  .. .. .. .. .. ..$ city   : chr "Stockholm"
  .. .. .. .. .. ..$ country: chr "Sweden"

  ..$ :List of 10
  .. ..$ id             : chr "774"
  .. ..$ firstname      : chr "Richard"
  .. ..$ surname        : chr "Axel"
  .. ..$ born           : chr "1946-07-02"
  .. ..$ died           : chr "0000-00-00"
  .. ..$ bornCountry    : chr "USA"
  .. ..$ bornCountryCode: chr "US"
  .. ..$ bornCity       : chr "New York, NY"
  .. ..$ gender         : chr "male"
  .. ..$ prizes         :List of 1
  .. .. ..$ :List of 5
  .. .. .. ..$ year        : chr "2004"
  .. .. .. ..$ category    : chr "medicine"
  .. .. .. ..$ share       : chr "2"
  .. .. .. ..$ motivation  : chr "\"for their discoveries of odorant receptors and the organization of the olfactory system\""
  .. .. .. ..$ affiliations:List of 1
  .. .. .. .. ..$ :List of 3
  .. .. .. .. .. ..$ name   : chr "Columbia University"
  .. .. .. .. .. ..$ city   : chr "New York, NY"
  .. .. .. .. .. ..$ country: chr "USA"

我应该如何将其转换为data.frame?

虽然列表中有列表,但我很乐意使用年份和类别,并免除奖品。

还有一个问题是并非每个记录都有相同数量的变量 - 这里的第二个例子,例如,不提供deathCountry字段,等等

TIA

道歉。我不应该在晚上这样做。提供的答案对我原来的问题很好。但是,当我运行完整列表时,我收到错误

Error in data.frame(year = "1931", category = "literature", share = "1",  : 
arguments imply differing number of rows: 1, 0

以下是导致此问题的数据。看起来它与附属关系有关

nobels <- list(structure(list(id = "359", firstname = "Axel Hugo Theodor", 
surname = "Theorell", born = "1903-07-06", died = "1982-08-15", 
bornCountry = "Sweden", bornCountryCode = "SE", bornCity = "Linköping", 
diedCountry = "Sweden", diedCountryCode = "SE", diedCity = "Stockholm", 
gender = "male", prizes = list(structure(list(year = "1955", 
category = "medicine", share = "1", motivation = "\"for his discoveries concerning the          nature and mode of action of oxidation enzymes\"", 
affiliations = list(structure(list(name = "Karolinska Institutet, Nobel Medical    Institute", 
city = "Stockholm", country = "Sweden"), .Names = c("name", 
"city", "country")))), .Names = c("year", "category", 
"share", "motivation", "affiliations")))), .Names = c("id", 
"firstname", "surname", "born", "died", "bornCountry", "bornCountryCode", 
"bornCity", "diedCountry", "diedCountryCode", "diedCity", "gender", 
"prizes")), structure(list(id = "604", firstname = "Erik Axel", 
surname = "Karlfeldt", born = "1864-07-20", died = "1931-04-08", 
bornCountry = "Sweden", bornCountryCode = "SE", bornCity = "Karlbo", 
diedCountry = "Sweden", diedCountryCode = "SE", diedCity = "Stockholm", 
gender = "male", prizes = list(structure(list(year = "1931", 
category = "literature", share = "1", motivation = "\"The poetry of Erik Axel   Karlfeldt\"", 
affiliations = list(list())), .Names = c("year", "category", 
"share", "motivation", "affiliations")))), .Names = c("id", 
"firstname", "surname", "born", "died", "bornCountry", "bornCountryCode", 
"bornCity", "diedCountry", "diedCountryCode", "diedCity", "gender", 
"prizes")))

2 个答案:

答案 0 :(得分:1)

正确识别后,由affiliations导致问题发生,其子列表为空列表。

> str(nobels)
List of 2
 $ :List of 13
  ..$ id             : chr "359"
  ..$ firstname      : chr "Axel Hugo Theodor"
  ..$ surname        : chr "Theorell"
  ..$ born           : chr "1903-07-06"
  ..$ died           : chr "1982-08-15"
  ..$ bornCountry    : chr "Sweden"
  ..$ bornCountryCode: chr "SE"
  ..$ bornCity       : chr "Linköping"
  ..$ diedCountry    : chr "Sweden"
  ..$ diedCountryCode: chr "SE"
  ..$ diedCity       : chr "Stockholm"
  ..$ gender         : chr "male"
  ..$ prizes         :List of 1
  .. ..$ :List of 5
  .. .. ..$ year        : chr "1955"
  .. .. ..$ category    : chr "medicine"
  .. .. ..$ share       : chr "1"
  .. .. ..$ motivation  : chr "\"for his discoveries concerning the          nature and mode of action of oxidation enzymes\""
  .. .. ..$ affiliations:List of 1
  .. .. .. ..$ :List of 3
  .. .. .. .. ..$ name   : chr "Karolinska Institutet, Nobel Medical    Institute"
  .. .. .. .. ..$ city   : chr "Stockholm"
  .. .. .. .. ..$ country: chr "Sweden"
 $ :List of 13
  ..$ id             : chr "604"
  ..$ firstname      : chr "Erik Axel"
  ..$ surname        : chr "Karlfeldt"
  ..$ born           : chr "1864-07-20"
  ..$ died           : chr "1931-04-08"
  ..$ bornCountry    : chr "Sweden"
  ..$ bornCountryCode: chr "SE"
  ..$ bornCity       : chr "Karlbo"
  ..$ diedCountry    : chr "Sweden"
  ..$ diedCountryCode: chr "SE"
  ..$ diedCity       : chr "Stockholm"
  ..$ gender         : chr "male"
  ..$ prizes         :List of 1
  .. ..$ :List of 5
  .. .. ..$ year        : chr "1931"
  .. .. ..$ category    : chr "literature"
  .. .. ..$ share       : chr "1"
  .. .. ..$ motivation  : chr "\"The poetry of Erik Axel   Karlfeldt\""
  .. .. ..$ affiliations:List of 1
  .. .. .. ..$ : list()                             **<--problem here**

如果您向该列表添加一些随机数据,则代码可以正常工作。

nobels[[2]]$prizes[[1]]$affiliations[[1]]<-list(name="random data")

使用plyr包:

library (plyr)
mydf <- ldply(nobels, data.frame)

答案 1 :(得分:1)

您还可以使用unnest

中的tidyr
 devtools::install_github("hadley/tidyr")
 library(tidyr)

使用您的新数据集,这似乎有效

  res1 <-unnest(lapply(nobels, function(x)
         as.data.frame.list(rapply(x,unlist), stringsAsFactors=FALSE)))

  str(res1)
  #Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    2 obs. of  19 variables:
  #   $ id                         : chr  "359" "604"
  #$ firstname                  : chr  "Axel Hugo Theodor" "Erik Axel"
  #$ surname                    : chr  "Theorell" "Karlfeldt"
  #$ born                       : chr  "1903-07-06" "1864-07-20"
  #$ died                       : chr  "1982-08-15" "1931-04-08"
  #$ bornCountry                : chr  "Sweden" "Sweden"
  #$ bornCountryCode            : chr  "SE" "SE"
  #$ bornCity                   : chr  "Linköping" "Karlbo"
  #$ diedCountry                : chr  "Sweden" "Sweden"
  #$ diedCountryCode            : chr  "SE" "SE"
  #$ diedCity                   : chr  "Stockholm" "Stockholm"
  #$ gender                     : chr  "male" "male"
  #$ prizes.year                : chr  "1955" "1931"
  #$ prizes.category            : chr  "medicine" "literature"
  #$ prizes.share               : chr  "1" "1"
  #$ prizes.motivation          : chr  "\"for his discoveries concerning the          nature and mode of action of oxidation enzymes\"" "\"The poetry of Erik Axel   Karlfeldt\""
  #$ prizes.affiliations.name   : chr  "Karolinska Institutet, Nobel Medical    Institute" NA
 #$ prizes.affiliations.city   : chr  "Stockholm" NA
 #$ prizes.affiliations.country: chr  "Sweden" NA