2

我需要将数据框转换为 R 中的 json 格式。我的数据框 y 是这样的:

dput(y)
structure(list(Name = structure(c(38L, 23L, 16L, 35L, 21L, 6L, 
34L, 15L, 46L, 1L, 43L, 28L, 39L, 27L, 7L, 20L, 14L, 44L, 48L, 
36L), .Label = c("server09", "server10", "server11", "server12", 
"server13", "server14", "server15", "server16", "server17", "server18", 
"server19", "server20", "server21", "server22", "server23", "server24", 
"server25", "server26", "server27", "server28", "server29", "server30", 
"server31", "server32", "server33", "server34", "server35", "server36", 
"server37", "server38", "server39", "server40", "server41", "server42", 
"server43", "server44", "server45", "server46", "server47", "server48", 
"server49", "server50", "server51", "server52", "server53", "server54", 
"server55", "server56", "server57", "server58"), class = "factor"), 
    Date = c(1372737600, 1372737602, 1372737609, 1372737617, 
    1372737618, 1372737618, 1372737643, 1372737646, 1372737648, 
    1372737652, 1372737654, 1372737660, 1372737665, 1372737671, 
    1372737699, 1372737701, 1372737718, 1372737721, 1372737728, 
    1372737731), Cpu = c(3.9025, 36.3042, 2.6075, 3.1338, 0.9474, 
    0.149, 5.4401, 2.5652, 0.3612, 3.2651, 1.8703, 13.8967, 4.2438, 
    5.4401, 2.468, 0.9147, 1.4637, 7.2528, 6.119, 7.7009)), .Names = c("Name", 
"Date", "Cpu"), row.names = c(1L, 42L, 83L, 84L, 125L, 126L, 
127L, 168L, 169L, 202L, 203L, 236L, 277L, 318L, 359L, 360L, 361L, 
362L, 395L, 396L), class = "data.frame")

我需要我的 json 文件看起来像这样:

[{
    "name":'server13',
    "data": [ [1372737600,3.9025], [1372737602,10], [1372737609,10] ... [1372737731,20] ]
}, {
    "name":'server14',
    "data": [ [1372737600,4], [1372737602,10], [1372737609,10] ... [1372737731,30] ]
}]

我使用 rjson 包如下:

p <- toJSON(as.list(y))

我得到这个输出:

"{\"Name\":[\"server13\",\"servar14\",...],\"Date\":[1372737600,1372737602,..],\"Cpu\":[3.9025,36.3042,..]}"

有没有一种简单的方法可以在 R 中做到这一点?

4

3 回答 3

2

你应该先转换你的列表

library(rjson)
toJSON(unname(by(dat,dat$Name,
            FUN=function(x)
              list(Name=x[,1],data=unname(x[,-1])))))

[1] "[{\"Name\":\"server09\",\"data\":[1372737652,3.2651]},
      {\"Name\":\"server14\",\"data\":[1372737618,0.149]},
      {\"Name\":\"server15\",\"data\":[1372737699,2.468]}
      ........
      {\"Name\":\"server56\",\"data\":[1372737728,6.119]},null,null]"

PS 请注意,这不适用于RJSONIO包,确实我收到此错误:

## Error: evaluation nested too deeply: infinite recursion /
## options(expressions=)?
于 2013-07-02T17:03:09.113 回答
1

从你的 data.frame 的结构我猜这只是你数据的一小部分,服务器有多个条目:

# split by server
require(RJSONIO)
servers <- split(y, y$Name)
dumFun <- function(x){
  sData <- servers[x][[1]]
  if(nrow(sData) >0){
    # create appropriate list
    dumList <- unname(apply(sData[,2:3], 1, function(y) unname(as.list(y))))
    return(list(name = x, data = dumList)) 
  }
}

jsData <- lapply(names(servers), dumFun)
jsInd <- sapply(jsData, is.null)
toJSON(jsData[!jsInd])

写入文件时

[{"name":"server09","data":[[1372737652,3.2651]]},{"name":"server14","data":[[1372737618,0.149]]},{"name": "server15","data":[[1372737699,2.468]]},{"name":"server22","data":[[1372737718,1.4637]]},{"name":"server23","data ":[[1372737646,2.5652]]},{"name":"server24","data":[[1372737609,2.6075]]},{"name":"server28","data":[[1372737701, 0.9147]]},{"name":"server29","data":[[1372737618,0.9474]]},{"name":"server31","data":[[1372737602,36.3042]]},{ "名称":"server35","数据":[[1372737671,5.4401]]},{"名称":"server36","数据":[[1372737660,13.8967]]},{"name":"server42","data":[[1372737643,5.4401]]},{"name":"server43","data":[[1372737617,3.1338]]},{ "名称":"server44","数据":[[1372737731,7.7009]]},{"名称":"server46","数据":[[1372737600,3.9025]]},{"名称":"server47 ","数据":[[1372737665,4.2438]]},{"名称":"server51","数据":[[1372737654,1.8703]]},{"名称":"server52","数据": [[1372737721,7.2528]]},{"name":"server54","data":[[1372737648,0.3612]]},{"name":"server56","data":[[1372737728,6.119] ]}]数据":[[1372737617,3.1338]]},{"name":"server44","data":[[1372737731,7.7009]]},{"name":"server46","data":[[1372737600 ,3.9025]]},{"name":"server47","data":[[1372737665,4.2438]]},{"name":"server51","data":[[1372737654,1.8703]]}, {"name":"server52","data":[[1372737721,7.2528]]},{"name":"server54","data":[[1372737648,0.3612]]},{"name":" server56","数据":[[1372737728,6.119]]}]数据":[[1372737617,3.1338]]},{"name":"server44","data":[[1372737731,7.7009]]},{"name":"server46","data":[[1372737600 ,3.9025]]},{"name":"server47","data":[[1372737665,4.2438]]},{"name":"server51","data":[[1372737654,1.8703]]}, {"name":"server52","data":[[1372737721,7.2528]]},{"name":"server54","data":[[1372737648,0.3612]]},{"name":" server56","数据":[[1372737728,6.119]]}]名称":"server51","数据":[[1372737654,1.8703]]},{"名称":"server52","数据":[[1372737721,7.2528]]},{"名称":"server54" ,"数据":[[1372737648,0.3612]]},{"名称":"server56","数据":[[1372737728,6.119]]}]名称":"server51","数据":[[1372737654,1.8703]]},{"名称":"server52","数据":[[1372737721,7.2528]]},{"名称":"server54" ,"数据":[[1372737648,0.3612]]},{"名称":"server56","数据":[[1372737728,6.119]]}]

于 2013-07-02T17:05:53.073 回答
1

我认为这种方法更简单

## take the first 3 observations
data <- head(y, 3)
data <- rbind(data,
           c(Name = "server24", Date = 1372737609, Cpu = 20.2345))

data
##        Name       Date     Cpu
## 1  server46 1372737600  3.9025
## 42 server31 1372737602 36.3042
## 83 server24 1372737609  2.6075
## 4  server24 1372737609 20.2345


## split the data
mylist <- split(data, data$Name)

## remove empty data.frame
mylist <- mylist[sapply(mylist, nrow) != 0]

## the ugliest part 
mylist <- unname(lapply(mylist, function(x)
                        list(name = unique(x[[1]]), 
                             data = matrix(x[2:3], nrow = 1))))

cat(RJSONIO::toJSON(mylist), "\n")


## [
##  {
##  "name": "server24",
## "data": [ [
##  [ "1372737609", "1372737609" ],
## [ "2.6075", "20.2345" ] 
## ] ] 
## },
## {
##  "name": "server31",
## "data": [ [
##  "1372737602",
## "36.3042" 
## ] ] 
## },
## {
##  "name": "server46",
## "data": [ [
##  "1372737600",
## "3.9025" 
## ] ] 
## } 
## ] 

已编辑

于 2013-07-02T17:33:59.413 回答