R:json到data.frame的向量
我有一个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","
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()