繁体   English   中英

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

[英]How can I create a data.frame from a nested list with differing numbers of variable

我已经下载了诺贝尔奖获得者的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?

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

还有一个问题,不是每个记录都具有相同数量的变量-例如,此处的第二个示例未提供dieCountry字段,

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")))

如您所正确确定的,问题是由于affiliations而产生的,其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)

你也可以使用unnesttidyr

 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM