You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将含15万条记录的复杂JSON转Dataframe遇1:nrow(test)错误求助

Fixing "Error in 1:nrow(test) : argument of length 0" When Converting JSON to DataFrame

Hey there, let's break down what's going wrong and fix your code step by step.

The Root Cause

That error pops up because your test object is a list (not a data frame)! Looking at your JSON structure, it's an array of objects—when you convert that to R (say with jsonlite::fromJSON()), you get a list, not a 2D data frame. nrow() only works on data frames/matrices, so calling nrow(test) returns NULL, which makes 1:nrow(test) throw that "argument of length 0" error.

Fixed Code & Explanations

Here's the adjusted code, with changes explained so you understand what's different:

Step 1: Correctly Iterate Over List Elements

Instead of 1:nrow(test), use seq_along(test) to loop through each element in your list. This works for both lists and data frames, so it's safer.

Step 2: Simplify Empty Response Checks

Your original code had odd indexing (test[[1]][[j]]) that wasn't targeting the right elements. Instead, we can directly check if each element in the test list is empty.

Step 3: Clean Up Data Frame Construction

We'll directly extract the nested daily data from valid responses and attach the corresponding reviewid cleanly.

# test <- Your JSON conversion result (e.g., from jsonlite::fromJSON())
reviewid <- c(98338143, 58929813, 65945346)

# Filter valid (non-empty) responses and null responses
valid <- which(sapply(test, function(x) length(x) > 0))
NullResponses <- which(sapply(test, function(x) length(x) == 0))

# Build list of data frames from valid responses
dflist <- lapply(valid, function(j) {
  # Extract the nested daily data from the j-th response
  daily_data <- test[[j]]$daily[[1]]
  # Create data frame with reviewid attached
  df <- data.frame(reviewid = reviewid[j], daily_data, stringsAsFactors = FALSE)
  df
})

# Combine all data frames into one
answer <- dplyr::bind_rows(dflist)

Pro Tips for Handling Large (150k Row) Data

Since you're working with 150k observations, here's a more efficient approach using purrr (avoids pre-filtering valid and is faster for big datasets):

library(purrr)

answer <- map_dfr(seq_along(test), function(j) {
  # Skip empty responses entirely
  if (length(test[[j]]) == 0) return(NULL)
  # Extract and format data
  daily_data <- test[[j]]$daily[[1]]
  data.frame(reviewid = reviewid[j], daily_data, stringsAsFactors = FALSE)
})

Also, when converting your JSON, use jsonlite::fromJSON(simplifyVector = FALSE) to keep the raw list structure—this avoids unexpected auto-conversions that can mess up your data. Running str(test) will help you visualize the structure if you get stuck later!

内容的提问来源于stack exchange,提问作者Evelien van der Waal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:00:49