R 将 Json 读入 data.frame
R Read Json into a data.frame
我正在尝试将 json
文件加载到 R
中的 data.frame
中。但是我的数据中有一些 list()
为空。
这是我的 json
数据:
json_file1 <- jsonlite::fromJSON('{"txtId":"20180101","data":{"user":[{"id":"123","phone":"00001","realName":"Eric","addr":{},"source":{},"registerDate":{},"type":0,"remain":{}}],"score":[]}}')
json_file2 <- jsonlite::fromJSON('{"txtId":"20180102","data":{"user":[{"id":"456","phone":"00002","realName":"Amy","addr":{},"source":{},"registerDate":{},"type":0,"remain":100}],"score":[]}}')
json_file = list(json_file1, json_file2)
zt.detail = lapply(json_file, function(y){
if(!is.null(y$data$user)) data.frame(y$data$user, stringsAsFactors = F)
})
当我 rbind
zt.detail
时,我得到错误:
# > dat_callrecord = data.table::rbindlist(zt.detail, fill = T)
# Error in data.table::rbindlist(zt.detail, fill = T) :
# Column 4 of item 1 is length 0, inconsistent with first column of that item which is length 1. rbind/rbindlist doesn't recycle as it already expects each item to be a uniform list, data.frame or data.table
# > str(zt.detail[[1]])
# 'data.frame': 1 obs. of 9 variables:
# $ id : chr "123"
# $ phone : chr "00001"
# $ realName : chr "Eric"
# $ addr :'data.frame': 1 obs. of 0 variables
# $ source :'data.frame': 1 obs. of 0 variables
# $ registerDate:'data.frame': 1 obs. of 0 variables
# $ type : int 0
# $ remain :'data.frame': 1 obs. of 0 variables
错误是因为我的数据结构包含 data.frame
个观察值但包含 0 个变量。所以我想把那些 list()
转移到 NA
之前得到以下结果:
> dat_callrecord
id phone realName type remain addr source registerDate
123 00001 Eric 0 NA NA NA NA
456 00002 Amy 0 100 NA NA NA
我们可以遍历list
,如果有data.frame
,将其替换为NA
,然后执行rbindlist
data.table::rbindlist(lapply(zt.detail, function(x) {
x[] <- lapply(x, function(y) if(is.data.frame(y)) NA else y)
x}))
# id phone realName addr source registerDate type remain
#1: 123 00001 Eric NA NA NA 0 NA
#2: 456 00002 Amy NA NA NA 0 100
我正在尝试将 json
文件加载到 R
中的 data.frame
中。但是我的数据中有一些 list()
为空。
这是我的 json
数据:
json_file1 <- jsonlite::fromJSON('{"txtId":"20180101","data":{"user":[{"id":"123","phone":"00001","realName":"Eric","addr":{},"source":{},"registerDate":{},"type":0,"remain":{}}],"score":[]}}')
json_file2 <- jsonlite::fromJSON('{"txtId":"20180102","data":{"user":[{"id":"456","phone":"00002","realName":"Amy","addr":{},"source":{},"registerDate":{},"type":0,"remain":100}],"score":[]}}')
json_file = list(json_file1, json_file2)
zt.detail = lapply(json_file, function(y){
if(!is.null(y$data$user)) data.frame(y$data$user, stringsAsFactors = F)
})
当我 rbind
zt.detail
时,我得到错误:
# > dat_callrecord = data.table::rbindlist(zt.detail, fill = T)
# Error in data.table::rbindlist(zt.detail, fill = T) :
# Column 4 of item 1 is length 0, inconsistent with first column of that item which is length 1. rbind/rbindlist doesn't recycle as it already expects each item to be a uniform list, data.frame or data.table
# > str(zt.detail[[1]])
# 'data.frame': 1 obs. of 9 variables:
# $ id : chr "123"
# $ phone : chr "00001"
# $ realName : chr "Eric"
# $ addr :'data.frame': 1 obs. of 0 variables
# $ source :'data.frame': 1 obs. of 0 variables
# $ registerDate:'data.frame': 1 obs. of 0 variables
# $ type : int 0
# $ remain :'data.frame': 1 obs. of 0 variables
错误是因为我的数据结构包含 data.frame
个观察值但包含 0 个变量。所以我想把那些 list()
转移到 NA
之前得到以下结果:
> dat_callrecord
id phone realName type remain addr source registerDate
123 00001 Eric 0 NA NA NA NA
456 00002 Amy 0 100 NA NA NA
我们可以遍历list
,如果有data.frame
,将其替换为NA
,然后执行rbindlist
data.table::rbindlist(lapply(zt.detail, function(x) {
x[] <- lapply(x, function(y) if(is.data.frame(y)) NA else y)
x}))
# id phone realName addr source registerDate type remain
#1: 123 00001 Eric NA NA NA 0 NA
#2: 456 00002 Amy NA NA NA 0 100