Json到Dataframe:错误:1:nrow(test)中的错误:长度为0的参数

时间:2019-05-30 09:56:50

标签: r json apply

我正在尝试将具有150.000个观察值的复杂JSON文件转换为数据框。有人慷慨地帮助我构造了代码,但是我遇到了同样的错误:Error in 1:nrow(test):长度为0的参数。我在社区中搜索了该主题,但是没有一个答案可以解决我的问题。欢迎任何提示!

这两行给出了错误:

valid<-which(sapply(1:nrow(test),  function(j) {length(test[[1]][[j]])}) >0)
NullResponses<-which(sapply(1:nrow(test),  function(j) {length(test[[1]][[j]])}) == 0)

json的结构:

enter image description here 这是完整的代码:

#test<- result from converting the JSON response.  
#vector of reviewid, used to make the initial request to the API
reviewid<-c(98338143, 58929813, 65945346)

#find only the responses that are not Null or blank
valid<-which(sapply(1:nrow(test),  function(j) {length(test[[1]][[j]])}) >0)
NullResponses<-which(sapply(1:nrow(test),  function(j) {length(test[[1]][[j]])}) == 0)

#create a list of data frames with the data from row of the response
dflist<-lapply( valid, function(j) {
  temp<-t(as.matrix(unlist(test[j,])))
  df<-data.frame(reviewid=reviewid[j], temp, stringsAsFactors = FALSE)
  df
})

#bind the rows together.
answer<-bind_rows(dflist)

Json:

 [{},
    {"daily":
       [{"time":"2010-03-18",
       "summary":"Partly cloudy throughout the day.",
       "icon":"partly-cloudy-day",
       "sunriseTime":"2010-03-18 07:22:51",
       "sunsetTime":"2010-03-18 19:25:28",
       "moonPhase":0.08,
       "precipIntensity":0,
       "precipIntensityMax":0,
       "precipProbability":0,
       "temperatureHigh":63.14,  
       "temperatureHighTime":1268928000,
       "temperatureLow":45.16,
       "temperatureLowTime":1268971200,
       "apparentTemperatureHigh":63.14,
       "apparentTemperatureHighTime":1268928000,
       "apparentTemperatureLow":45.16,
       "apparentTemperatureLowTime":1268971200,
       "dewPoint":36.97,
       "humidity":0.58,
       "pressure":1025.96,
       "windSpeed":1.24,
       "windGust":7.87, 
       "windGustTime":1268866800,
       "windBearing":48,
       "cloudCover":0.54,
       "uvIndex":5,
       "uvIndexTime":1268913600,
       "visibility":6.19,
       "temperatureMin":43.97,
       "temperatureMinTime":"2010-03-18 07:00:00",
       "temperatureMax":63.14,
       "temperatureMaxTime":"2010-03-18 17:00:00",
       "apparentTemperatureMin":42.03,
       "apparentTemperatureMinTime":"2010-03-18 08:00:00",
       "apparentTemperatureMax":63.14,
       "apparentTemperatureMaxTime":"2010-03-18 17:00:00"}]},

    {"daily":
       [{"time":"2010-05-30 01:00:00",
       "summary":"Mostly cloudy until evening.",
       "icon":"partly-cloudy-day",
       "sunriseTime":"2010-05-30 05:38:39",
       "sunsetTime":"2010-05-30 22:44:55",
       "moonPhase":0.58,
       "precipIntensity":0.0038,
       "precipIntensityMax":0.0766,
       "precipIntensityMaxTime”:"2010-05-30 04:00:00",
       "precipProbability":1,
       "precipType":"rain", 
       "temperatureHigh":58.99,
       "temperatureHighTime":1275242400, 
       "temperatureLow":36.62,  
       "temperatureLowTime":1275278400, 
       "apparentTemperatureHigh":58.99, 
       "apparentTemperatureHighTime":1275242400, 
       "apparentTemperatureLow":36.62,
       "apparentTemperatureLowTime":1275278400,
       "dewPoint":43.61,
       "humidity":0.76,
       "pressure":1011.52,
       "windSpeed":4.65,
       "windGust":21.4,
       "windGustTime":1275224400,
       "windBearing":350,
       "cloudCover":0.61,
       "uvIndex":5,
       "uvIndexTime":1275213600,
       "visibility":5.85, 
       "temperatureMin":45.99,
       "temperatureMinTime":"2010-05-30 07:00:00",
       "temperatureMax":58.99,
       "temperatureMaxTime":"2010-05-30 20:00:00",
       "apparentTemperatureMin":43.31,
       "apparentTemperatureMinTime":"2010-05-30 06:00:00",
       "apparentTemperatureMax":58.99,
       "apparentTemperatureMaxTime":"2010-05-30 20:00:00"}]}]

1 个答案:

答案 0 :(得分:0)

这是因为转换后的json是列表列表,不适用于nrow()功能。您可以尝试使用此代码来检查结果是否符合您的预期吗?

valid         <- which(sapply(1:NROW(test),  function(j) {length(test[[j]])}) > 0)
NullResponses <- which(sapply(1:NROW(test),  function(j) {length(test[[j]])}) == 0)

dflist <- lapply(valid, function(j) {
  temp <- t(as.matrix(unlist(test[j])))
  df   <- data.frame(reviewid = reviewid[j], temp, stringsAsFactors = FALSE)
  df
})

answer <- bind_rows(dflist)