Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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到data.frame的向量_Json_R_Jsonlite - Fatal编程技术网

R:json到data.frame的向量

R:json到data.frame的向量,json,r,jsonlite,Json,R,Jsonlite,我有一个JSON向量(结构相同),并将其转换为data.frame。下面的例子正是我想要的 require(jsonlite) # fromJSON() require(magrittr) # for the pipeline only require(data.table) # rbindlist() jsons <- c('{"num":1,"char":"a","list":{"x":1,"y":2}}', '{"num":2,"char":"b","

我有一个JSON向量(结构相同),并将其转换为data.frame。下面的例子正是我想要的

require(jsonlite)   # fromJSON()
require(magrittr)   # for the pipeline only
require(data.table) # rbindlist()

jsons <- c('{"num":1,"char":"a","list":{"x":1,"y":2}}',
           '{"num":2,"char":"b","list":{"x":1,"y":2}}',
           '{"num":3,"char":"c","list":{"x":1,"y":2}}')

df <- jsons %>%
  lapply(fromJSON) %>%
  lapply(as.data.frame.list, stringsAsFactors = F) %>%
  rbindlist(fill = T)
我知道有很多类似的问题,但我看到的大多数答案都是关于使用
as.data.frame()
data.frame()
。没有人提到速度。也许没有更好的解决办法了。

我终于找到了答案。它将在CRAN上


这个函数比我上面的例子快10倍左右。

尝试将所有JSON折叠到一个字符串中。让我们展示解决方案的示例:

require(jsonlite)
要求(数据表)
json
json <- '{"$schema":"http://json-schema.org/draft-04/schema#","title":"Product set","type":"array","items":{"title":"Product","type":"object","properties":{"id":{"description":"The unique identifier for a product","type":"number"},"name":{"type":"string"},"price":{"type":"number","minimum":0,"exclusiveMinimum":true},"tags":{"type":"array","items":{"type":"string"},"minItems":1,"uniqueItems":true},"dimensions":{"type":"object","properties":{"length":{"type":"number"},"width":{"type":"number"},"height":{"type":"number"}},"required":["length","width","height"]},"warehouseLocation":{"description":"Coordinates of the warehouse with the product","$ref":"http://json-schema.org/geo"}},"required":["id","name","price"]}}'
system.time(
  df <- json %>% rep(1000) %>%
    lapply(fromJSON) %>%
    lapply(as.data.frame.list, stringsAsFactors = F) %>%
    rbindlist(fill = T)
) # 2.72
devtools::install_github("jeremystan/tidyjson")
tidyjson::spread_all()