【发布时间】:2014-10-16 06:11:33
【问题描述】:
我已经下载了一个诺贝尔奖获得者的 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?
虽然列表中有列表,但我很乐意使用年份和类别,而无需奖品。
还有一个问题,不是每条记录都有相同数量的变量 - 例如这里的第二个例子,没有提供 deadCountry 字段等
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")))
【问题讨论】:
-
请添加
dput(head(your_list)。 -
@pssguy 最好显示您的数据集的
dput(正如我在帖子中所展示的那样)以获得数据结构的准确表示。 -
@akrun。抱歉,我忘记了 dput。谢谢建议
-
@pssguy 请检查我的更新。
标签: r list nested-lists