我有这些数据:
x<-c("{\"data\": [{\"target\": \"cpu\", \"datapoints\": [[5, 1423825200000.0], [10, 1423825500000.0], [3, 1423825800000.0], [20, 1423826100000.0], [15, 1423826400000.0], [50, 1423826700000.0], [11, 1423827000000.0], [50, 1423827300000.0], [60, 1423827600000.0], [50, 1423827900000.0], [60, 1423828200000.0], [10, 1423828500000.0], [100, 1423828800000.0], [20, 1423829100000.0], [40, 1423829400000.0], [45, 1423829700000.0], [35, 1423830000000.0], [33, 1423830300000.0], [22, 1423830600000.0], [45, 1423830900000.0], [70, 1423831200000.0], [34, 1423831500000.0], [30, 1423831800000.0]]},{\"target\": \"login\", \"datapoints\": [[5000, 1423825200000.0], [100000, 1423825500000.0], [3000, 1423825800000.0], [20000, 1423826100000.0], [15, 1423826400000.0], [50, 1423826700000.0], [11, 1423827000000.0], [50, 1423827300000.0], [60, 1423827600000.0], [50, 1423827900000.0], [60000, 1423828200000.0], [10000, 1423828500000.0], [100000, 1423828800000.0], [20000, 1423829100000.0], [40000, 1423829400000.0], [45000, 1423829700000.0], [35000, 1423830000000.0], [33000, 1423830300000.0], [22000, 1423830600000.0], [45000, 1423830900000.0], [70000, 1423831200000.0], [34000, 1423831500000.0], [30000, 1423831800000.0]]}]}")
我需要将这个json数据转换为具有正确列名的数据框:
我试过了:
x<-fromJSON(x)
(x <- lapply(x, function(x) { as.data.frame(x) }))
(x <- do.call(rbind, x))
dat<-cbind(data.frame(x$data[[1]]),data.frame(x$data[[2]])
但我没有看到标题cpu和登录。我需要能够通过dat $ cpu或dat $ login
来引用每一列我得到这样的东西:
structure(list(X1 = c(5, 10, 3, 20, 15, 50, 11, 50, 60, 50, 60,
10, 100, 20, 40, 45, 35, 33, 22, 45, 70, 34, 30), X2 = c(1423825200000,
1423825500000, 1423825800000, 1423826100000, 1423826400000, 1423826700000,
1.423827e+12, 1423827300000, 1423827600000, 1423827900000, 1423828200000,
1423828500000, 1423828800000, 1423829100000, 1423829400000, 1423829700000,
1.42383e+12, 1423830300000, 1423830600000, 1423830900000, 1423831200000,
1423831500000, 1423831800000), X1 = c(5000, 1e+05, 3000, 20000,
15, 50, 11, 50, 60, 50, 60000, 10000, 1e+05, 20000, 40000, 45000,
35000, 33000, 22000, 45000, 70000, 34000, 30000), X2 = c(1423825200000,
1423825500000, 1423825800000, 1423826100000, 1423826400000, 1423826700000,
1.423827e+12, 1423827300000, 1423827600000, 1423827900000, 1423828200000,
1423828500000, 1423828800000, 1423829100000, 1423829400000, 1423829700000,
1.42383e+12, 1423830300000, 1423830600000, 1423830900000, 1423831200000,
1423831500000, 1423831800000)), .Names = c("X1", "X2", "X1",
"X2"), row.names = c(NA, -23L), class = "data.frame")
有没有更好的方法将此json转换为具有正确列名的数据框?
我需要输出如下:
structure(list(cpu = c(5, 10, 3, 20, 15, 50), cpu.time = c(1423825200000,
1423825500000, 1423825800000, 1423826100000, 1423826400000, 1423826700000
), login = c(5000, 1e+05, 3000, 20000, 15, 50), login.time = c(1423825200000,
1423825500000, 1423825800000, 1423826100000, 1423826400000, 1423826700000
)), .Names = c("cpu", "cpu.time", "login", "login.time"
), row.names = c(NA, 6L), class = "data.frame")
答案 0 :(得分:1)
这应该有效
xx <- jsonlite:::fromJSON(x)
Reduce(cbind, Map(function(n, d)
setNames(data.frame(d), c(n, paste0(n,".time"))),
xx$data[[1]], xx$data[[2]])
)
返回
cpu cpu.time login login.time
1 5 1.423825e+12 5000 1.423825e+12
2 10 1.423825e+12 100000 1.423825e+12
3 3 1.423826e+12 3000 1.423826e+12
4 20 1.423826e+12 20000 1.423826e+12
5 15 1.423826e+12 15 1.423826e+12
6 50 1.423827e+12 50 1.423827e+12