Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:json数组对象的数据帧_Json_R_Rjson - Fatal编程技术网

R:json数组对象的数据帧

R:json数组对象的数据帧,json,r,rjson,Json,R,Rjson,我试图从数据帧中获取JSON数组对象,其中每个JSON对象都是数据帧的子集 > x <- 1:5 > y <-c('a','b','c','d','e') > z <-c(1,1,1,2,2) > df <-data.frame(x,y,z) > df x y z 1 1 a 1 2 2 b 1 3 3 c 1 4 4 d 2 5 5 e 2 > rjson::to

我试图从数据帧中获取JSON数组对象,其中每个JSON对象都是数据帧的子集

> x <- 1:5  
> y <-c('a','b','c','d','e')  
> z <-c(1,1,1,2,2)  
> df <-data.frame(x,y,z)  
> df  
    x y z  
  1 1 a 1  
  2 2 b 1  
  3 3 c 1  
  4 4 d 2  
  5 5 e 2  
> rjson::toJSON(df)  
[1] "{\"x\":[1,2,3,4,5],\"y\":[\"a\",\"b\",\"c\",\"d\",\"e\"],\"z\":[1,1,1,2,2]}"  
> df1 = toJSONArray2(na.omit(df), json = F, names = F)  
> rjson::toJSON(df1)  
[1] "[[1,\"a\",1],[2,\"b\",1],[3,\"c\",1],[4,\"d\",2],[5,\"e\",2]]"  
>x y z df
x y z
1 a 1
2 b 1
3 c 1
4D2
5 e 2
>rjson::toJSON(df)
[1] “{\'x\':[1,2,3,4,5],\'y\':[\'a\',\'b\',\'c\',\'d\',\'e\'],\'z\':[1,1,1,2,2]}”
>df1=toJSONArray2(na.omit(df),json=F,names=F)
>rjson::toJSON(df1)
[1] “[1,\'a\'、1]、[2,\'b\'、1]、[3,\'c\'、1]、[4,\'d\'、2]、[5,\'e\'、2]。”
我需要的输出是

[1,a],[2,b],[3,c],[4,d],[5,e]]

在下面的方法中,我能够按预期获得数据帧列表,但无法获得所需的json输出

> x <- foreach(i=1:2) %do% { subset(df,df$z==i)[c(1,2)]}  
> x  
 [[1]]   
   x y  
 1 1 a  
 2 2 b  
 3 3 c  

 [[2]]
   x y
 4 4 d
 5 5 e
>x
[[1]]   
xy
11A
2 b
3 c
[[2]]
xy
4d
5 e
找到了解决办法

> x <- foreach(i=1:2) %do% {
   tmp <-subset(df,df$z==i)[c(1,2)]  
   toJSONArray2(na.omit(tmp), json = F, names = F)  
   }
> rjson::toJSON(x) 

>x对于其他试图回答的人,toJSONArray[2]
函数位于
rCharts
包中。您的解决方案非常紧凑,但可以使用
sapply
split
来解环和收紧一点:

library(rjson)
library(rCharts)

x <- 1:5  
y <- c('a', 'b' ,'c' ,'d' ,'e')  
z <- c(1, 1, 1, 2, 2)  

df <- data.frame(x, y, z) 

toJSON(df)

out <- toJSONArray(sapply(split(df[,1:2], df$z), function(x) {
  toJSONArray2(x, names=FALSE, json = FALSE)
}))

# doing gsub only for SO example output
cat(gsub("\\n", "", out))

## [ [ [ 1,"a" ],[ 2,"b" ],[ 3,"c" ] ],[ [ 4,"d" ],[ 5,"e" ] ] ]
这些函数都经过了非常优化,但是
toJSONArray2
基本上使用了一个
apply
函数作为
for
循环,所以让我们看看JSON的自编码是否更适合您的需要。以下内容对您来说可能更快,但您可能需要对生产代码进行更多的调整(如果您需要去掉引号的整数):


outrCharts
中的
toJSONArray2
功能运行缓慢,主要原因是使用了
RJSONIO
。我正在使用
rjson
将其更新为更快的实现。这是我到目前为止所拥有的。我从
pandas
中借用了
orient
参数的思想

to_json = function(df, orient = "columns", json = T){
  dl = as.list(df)
  dl = switch(orient, 
    columns = dl,
    records = do.call('zip_vectors_', dl),
    values = do.call('zip_vectors_', setNames(dl, NULL))
  )
  if (json){
    dl = rjson::toJSON(dl)
  }
  return(dl)
}

zip_vectors_ = function(..., names = F){
  x = list(...)
  y = lapply(seq_along(x[[1]]), function(i) lapply(x, pluck_(i)))
  if (names) names(y) = seq_along(y)
  return(y)
}

pluck_ = function (element){
  function(x) x[[element]]
}
下面的示例将向您展示
to_json
to jsonarray2
快20倍,其中大部分是由于使用了
rjson
而不是
RJSONIO

N = 10^3

df <- data.frame(
  x = rpois(N, 10),
  y = sample(LETTERS, N, replace = T),
  z = rpois(N, 5)
)

library(microbenchmark)
autoplot(microbenchmark(
  to_json(df, orient = "values", json = T),
  toJSONArray2(df, names = F),
  times = 5
))

谢谢:-)。有没有其他没有toJSONArray2的实现?这个函数非常慢(在100000个元素的数据集上,仅此函数就占用了大约300秒80%的处理时间),这要感谢Ramnath。通过to_json实现,我能够将执行时间从370秒减少到10秒:-)太棒了!对于我来说,这是一个很好的用例,可以将
的更新实现转换为\u json
to_json = function(df, orient = "columns", json = T){
  dl = as.list(df)
  dl = switch(orient, 
    columns = dl,
    records = do.call('zip_vectors_', dl),
    values = do.call('zip_vectors_', setNames(dl, NULL))
  )
  if (json){
    dl = rjson::toJSON(dl)
  }
  return(dl)
}

zip_vectors_ = function(..., names = F){
  x = list(...)
  y = lapply(seq_along(x[[1]]), function(i) lapply(x, pluck_(i)))
  if (names) names(y) = seq_along(y)
  return(y)
}

pluck_ = function (element){
  function(x) x[[element]]
}
N = 10^3

df <- data.frame(
  x = rpois(N, 10),
  y = sample(LETTERS, N, replace = T),
  z = rpois(N, 5)
)

library(microbenchmark)
autoplot(microbenchmark(
  to_json(df, orient = "values", json = T),
  toJSONArray2(df, names = F),
  times = 5
))
library(dplyr)

dfl = df %.%
  group_by(z) %.%
  do(function(x){
    to_json(x[-3], orient = 'values', json = F)  
  })