为什么我没有得到这个for循环返回的预期列表?

时间:2017-04-12 04:37:56

标签: json r for-loop dataframe

我有一个小的JSON对象

j <- list(data= list(c(1489363200000, 186841676082516), c(1489449600000, 186318018993681
    ), c(1489536000000, 195610680688542), c(1489622400000, 197567887268771  
    ), c(1489708800000, 200901384171685), c(1489795200000, 138710699233524
    ), c(1489881600000, 122137578497247), c(1489968000000, 187609138951695
    ), c(1490054400000, 221990103719893), c(1490140800000, 237679711895307
    ), c(1490227200000, 203517279876234), c(1490313600000, 197617683034249
    ), c(1490400000000, 130778992561863), c(1490486400000, 122840062298549
    ), c(1490572800000, 194332605707575), c(1490659200000, 198901100827757
    ), c(1490745600000, 200128701898347), c(1490832000000, 222814133876696
    ), c(1490918400000, 210223087952673), c(1491004800000, 305686669170214
    ), c(1491091200000, 288755720186627), c(1491177600000, 286806683689669
    ), c(1491264000000, 216426591598647), c(1491350400000, 272193687564242
    ), c(1491436800000, 233995158271699), c(1491523200000, 216390247091183
    ), c(1491609600000, 228342701035144), c(1491696000000, 183517184406659
    ), c(1491782400000, 224070387545431), c(1491868800000, 206737520263679
    ), c(1491955200000, 30417143436121)),
 time = structure(list(start = 1489363200000, startTime = "2017-03-13T00:00:00.00Z", 
    end = 1492041600000, endTime = "2017-04-13T00:00:00.00Z", 
    interval = "1 DAY"), .Names = c("start", "startTime", "end", 
"endTime", "interval"))
)

我试图像data.frame一样存储,如此:

df_2 <- data.frame(
        Date = seq.Date(as.Date(j$time$startTime), as.Date(j$time$endTime), by = 'days', origin = '1970-01-01'),
        Metric = for (i in seq_along(j$data)) print(j$data[[i]][2])
      )

会抛出不匹配的行号(arguments imply differing number of rows: 32, 0)。

当我在Metric的调用之外运行时,data.frame列中的 > x <- for (i in seq_along(j$data)) print(j$data[[i]][2]) [1] 186841676082516 [1] 186318018993681 [1] 195610680688542 [1] 197567887268771 [1] 200901384171685 [1] 138710699233524 [1] 122137578497247 [1] 187609138951695 [1] 221990103719893 [1] 237679711895307 [1] 203517279876234 [1] 197617683034249 [1] 130778992561863 [1] 122840062298549 [1] 194332605707575 [1] 198901100827757 [1] 200128701898347 [1] 222814133876696 [1] 210223087952673 [1] 305686669170214 [1] 288755720186627 [1] 286806683689669 [1] 216426591598647 [1] 272193687564242 [1] 233995158271699 [1] 216390247091183 [1] 228342701035144 [1] 183517184406659 [1] 224070387545431 [1] 206737520263679 [1] 30417143436121 > x NULL > 列如下所示:

data.frame

在调用[1]时,我在列表构建中忽略了什么?我怀疑return(...) s的长索引是一个线索,而使用print(...)代替append并不起作用... ...

0 个答案:

没有答案