0

我有一个名为 data 的变量,其中包含 json 格式的数据:

dput(data)

{\"数据\": [{\"目标\": \"in\", \"数据点\": [[5, 1423825200000.0], [0, 1423825500000.0], [0, 1423825800000.0], [0, 1423826100000.0 ],[0,1423826400000.0],[0,1423826700000.0],[0,14238270000.0],[0,1423827300000.0],[0,1423827600000.0] [1,1423828800000.0],[0,1423829100000.0],[0,1423829400000.0],[0,1423829700000.0] ,1423831200000.0],[0,1423831500000.0],[0,1423831800000.0],[0,1423832100000.0],[0,1423832400000.0] ], [0, 1423833900000.0], [0, 1423834200000.0], [0, 1423834500000.0], [1, 1423834800000.0], [0, 1423835100000.0], [4, 1423],83540000000.[9,1423835700000.0],[1,14238360000.0],[3,1423836300000.0],[0,1423836600000.0],[1,1423836900000.0] ,1423838100000.0],[0,1423838400000.0],[0,1423838700000.0],[0,14238390000.0],[0,1423839300000.0] ],[0,1423840800000.0],[0,1423841100000.0],[0,1423841400000.0],[0,1423841700000.0],[0,14238420000.0] [0,1423843200000.0],[0,1423843500000.0],[0,1423843800000.0],[0,1423844100000.0],[0,14238444400000.0] , 1423845600000.0], [0, 1423845900000.0], [0, 1423846200000.0], [0, 1423846500000.0], [0, 1423846800000.0], [0, 1423847100000.0],[0,1423847400000.0],[0,1423847700000.0],[0,142384880000.0],[0,1423848300000.0],[0,1423848600000.0] : \"out\", \"datapoints\": [[5, 1423825200000.0], [0, 1423825500000.0], [0, 1423825800000.0], [0, 1423826100000.0], [0, 1423826400000.0], [00, 4], [0826] [0,14238270000.0],[0,1423827300000.0],[0,1423827600000.0],[0,1423827900000.0],[0,1423828200000.0] ,1423829400000.0],[0,1423829700000.0],[0,1423830000000.0],[0,1423830300000.0],[0,1423830600000.0] ], [0, 1423832100000.0], [0, 1423832400000.0], [0, 1423832700000.0], [0, 1423833000000.0], [0, 1423833300000.0], [0, 1423833600000.0],[0,1423833900000.0],[0,1423834200000.0],[0,1423834500000.0],[1,1423834800000.0],[1,1423834800000.0],[0,1423835100000.0] ,[3,1423836300000.0],[0,1423836600000.0],[1,1423836900000.0],[1,1423837200000.0],[1,1423837200000.0],[0,1423837500000.0] 0,1423838700000.0],[0,14238390000.0],[0,14238393300000.0],[0,1423839600000.0],[0,1423839900000.0] 1423841100000.0],[0,1423841400000.0],[0,1423841700000.0],[0,1423842000000.0],[0,1423842300000.0] , [0, 1423843800000.0], [0, 1423844100000.0], [0, 1423844400000.0], [0, 1423844700000.0], [0, 1423845000000.0], [0, 1423845300000.0],[0,1423845600000.0],[0,1423845900000.0],[0,1423846200000.0],[0,1423846500000.0],[0,1423846800000.0] , [0, 1423848000000.0], [0, 1423848300000.0], [0, 1423848600000.0], [1, 1423848900000.0], [1, 1423849200000.0]]}]}

我需要将此数据转换为 R 中的数据框。

DateTime In Out
1423848600000.0  N/A  0
1423848300000.0  N/A  4
etc

我尝试使用这个:

require(jsonlite)
dat <- fromJSON(data)

数据看起来像这样:

$at
  arge
1   NA
2   NA
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    atapoint
1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       NULL
2 5.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 1.000000e+00, 0.000000e+00, 4.000000e+00, 9.000000e+00, 1.000000e+00, 3.000000e+00, 0.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 2.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 1.000000e+00, 1.000000e+00, 1.423825e+12, 1.423825e+12, 1.423826e+12, 1.423826e+12, 1.423826e+12, 1.423827e+12, 1.423827e+12, 1.423827e+12, 1.423828e+12, 1.423828e+12, 1.423828e+12, 1.423829e+12, 1.423829e+12, 1.423829e+12, 1.423829e+12, 1.423830e+12, 1.423830e+12, 1.423830e+12, 1.423831e+12, 1.423831e+12, 1.423831e+12, 1.423831e+12, 1.423832e+12, 1.423832e+12, 1.423832e+12, 1.423833e+12, 1.423833e+12, 1.423833e+12, 1.423834e+12, 1.423834e+12, 1.423834e+12, 1.423834e+12, 1.423835e+12, 1.423835e+12, 1.423835e+12, 1.423836e+12, 1.423836e+12, 1.423836e+12, 1.423837e+12, 1.423837e+12, 1.423837e+12, 1.423838e+12, 1.423838e+12, 1.423838e+12, 1.423838e+12, 1.423839e+12, 1.423839e+12, 1.423839e+12, 1.423840e+12, 1.423840e+12, 1.423840e+12, 1.423841e+12, 1.423841e+12, 1.423841e+12, 1.423841e+12, 1.423842e+12, 1.423842e+12, 1.423842e+12, 1.423843e+12, 1.423843e+12, 1.423843e+12, 1.423843e+12, 1.423844e+12, 1.423844e+12, 1.423844e+12, 1.423845e+12, 1.423845e+12, 1.423845e+12, 1.423846e+12, 1.423846e+12, 1.423846e+12, 1.423847e+12, 1.423847e+12, 1.423847e+12, 1.423847e+12, 1.423848e+12, 1.423848e+12, 1.423848e+12, 1.423849e+12, 1.423849e+12, 1.423849e+12
4

1 回答 1

1

这是一般的想法:

out <- NULL
for(i in seq.int(length(x$data))){

    mx <- do.call(rbind,x$data[[2]]$datapoints)

    #>       [,1]         [,2]
    #>  [1,]    5 1.423825e+12
    #>  [2,]    0 1.423825e+12
    #>  [3,]    0 1.423826e+12
    ...

    df <- as.data.frame(x$data[[2]]$target)
    names(df) <- c('out','date')

    out <- cbind(out,df)
}

请注意,这实际上不会针对您当前的示例运行,因为x$data[[2]]$target如果此值的长度与 x$data[[2]]$datapoints 中的值的长度不一致

于 2015-02-18T17:45:47.803 回答