将导入的json数据导入数据框

我有一个文件,其中包含要在R中使用的1500个json对象。我已经能够将数据作为列表导入,但是在将其强制转换为有用的结构时遇到了麻烦。我想创建一个数据框,其中每个json对象包含一行,每个key:value对包含一列。


我用这个小的假数据集重新创建了我的处境:


[{"name":"Doe, John","group":"Red","age (y)":24,"height (cm)":182,"wieght (kg)":74.8,"score":null},

{"name":"Doe, Jane","group":"Green","age (y)":30,"height (cm)":170,"wieght (kg)":70.1,"score":500},

{"name":"Smith, Joan","group":"Yellow","age (y)":41,"height (cm)":169,"wieght (kg)":60,"score":null},

{"name":"Brown, Sam","group":"Green","age (y)":22,"height (cm)":183,"wieght (kg)":75,"score":865},

{"name":"Jones, Larry","group":"Green","age (y)":31,"height (cm)":178,"wieght (kg)":83.9,"score":221},

{"name":"Murray, Seth","group":"Red","age (y)":35,"height (cm)":172,"wieght (kg)":76.2,"score":413},

{"name":"Doe, Jane","group":"Yellow","age (y)":22,"height (cm)":164,"wieght (kg)":68,"score":902}]

数据的一些功能:


所有对象都包含相同数量的key:value对,尽管某些值是null

每个对象有两个非数字列(名称和组)

名称是唯一标识符,大约有10个组

名称和组整体中的许多都包含空格,逗号和其他标点符号。

基于这个问题:R list(structure(list()))到数据帧,我尝试了以下操作:


json_file <- "test.json"

json_data <- fromJSON(json_file)

asFrame <- do.call("rbind.fill", lapply(json_data, as.data.frame))

有了我的真实数据和虚假数据,最后一行给我这个错误:


Error in data.frame(name = "Doe, John", group = "Red", `age (y)` = 24,  : 

  arguments imply differing number of rows: 1, 0


繁花不似锦
浏览 546回答 3
3回答

犯罪嫌疑人X

您只需要将NA替换为NULL:require(RJSONIO)&nbsp; &nbsp;&nbsp;json_file <-&nbsp; '[{"name":"Doe, John","group":"Red","age (y)":24,"height (cm)":182,"wieght (kg)":74.8,"score":null},&nbsp; &nbsp; {"name":"Doe, Jane","group":"Green","age (y)":30,"height (cm)":170,"wieght (kg)":70.1,"score":500},&nbsp; &nbsp; {"name":"Smith, Joan","group":"Yellow","age (y)":41,"height (cm)":169,"wieght (kg)":60,"score":null},&nbsp; &nbsp; {"name":"Brown, Sam","group":"Green","age (y)":22,"height (cm)":183,"wieght (kg)":75,"score":865},&nbsp; &nbsp; {"name":"Jones, Larry","group":"Green","age (y)":31,"height (cm)":178,"wieght (kg)":83.9,"score":221},&nbsp; &nbsp; {"name":"Murray, Seth","group":"Red","age (y)":35,"height (cm)":172,"wieght (kg)":76.2,"score":413},&nbsp; &nbsp; {"name":"Doe, Jane","group":"Yellow","age (y)":22,"height (cm)":164,"wieght (kg)":68,"score":902}]'json_file <- fromJSON(json_file)json_file <- lapply(json_file, function(x) {&nbsp; x[sapply(x, is.null)] <- NA&nbsp; unlist(x)})一旦每个元素都有一个非空值,就可以调用rbind而不会出现错误:do.call("rbind", json_file)&nbsp; &nbsp; &nbsp;name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;group&nbsp; &nbsp; age (y) height (cm) wieght (kg) score[1,] "Doe, John"&nbsp; &nbsp; "Red"&nbsp; &nbsp; "24"&nbsp; &nbsp; "182"&nbsp; &nbsp; &nbsp; &nbsp;"74.8"&nbsp; &nbsp; &nbsp; NA&nbsp; &nbsp;[2,] "Doe, Jane"&nbsp; &nbsp; "Green"&nbsp; "30"&nbsp; &nbsp; "170"&nbsp; &nbsp; &nbsp; &nbsp;"70.1"&nbsp; &nbsp; &nbsp; "500"[3,] "Smith, Joan"&nbsp; "Yellow" "41"&nbsp; &nbsp; "169"&nbsp; &nbsp; &nbsp; &nbsp;"60"&nbsp; &nbsp; &nbsp; &nbsp; NA&nbsp; &nbsp;[4,] "Brown, Sam"&nbsp; &nbsp;"Green"&nbsp; "22"&nbsp; &nbsp; "183"&nbsp; &nbsp; &nbsp; &nbsp;"75"&nbsp; &nbsp; &nbsp; &nbsp; "865"[5,] "Jones, Larry" "Green"&nbsp; "31"&nbsp; &nbsp; "178"&nbsp; &nbsp; &nbsp; &nbsp;"83.9"&nbsp; &nbsp; &nbsp; "221"[6,] "Murray, Seth" "Red"&nbsp; &nbsp; "35"&nbsp; &nbsp; "172"&nbsp; &nbsp; &nbsp; &nbsp;"76.2"&nbsp; &nbsp; &nbsp; "413"[7,] "Doe, Jane"&nbsp; &nbsp; "Yellow" "22"&nbsp; &nbsp; "164"&nbsp; &nbsp; &nbsp; &nbsp;"68"&nbsp; &nbsp; &nbsp; &nbsp; "902"
打开App,查看更多内容
随时随地看视频慕课网APP