无法将JSON转换为数据框 [英] Unable to convert JSON to dataframe
问题描述
链接< - >我想将json文件转换为R中的数据框。 'https://www.dropbox.com/s/ckfn1fpkcix1ccu/bevingenbag.json'
文件< - fromJSON(file = link,method ='C')
bev< - do.call (cbind,文件)
我得到这个:
类型特征
1 FeatureCollection列表(type =Feature,geometry = list(type =Point,coordinates = c(6.54800000288927,52.9920000044505) ),properties = list(gid =1496600,yymmdd =19861226,lat =52.992,lon =6.548,mag =2.8,depth =1.0,knmilocatie =Assen,baglocatie = Assen,tijd =74751))
这是矩阵的第一行。所有其他行都具有相同的结构。我对 properties = list(gid =1496600,yymmdd =19861226,lat =52.992,lon =6.548,mag =2.8,depth =1.0 ,knmilocatie =Assen,baglocatie =Assen,tijd =74751)
部分,应转换为数据框,列为 gid,yymmdd,lat我的搜索和尝试了几个解决方案,但没有一个工作。我用这个rjson包。我也尝试过RJSONIO& jsonlite包,但无法提取所需的信息。
任何人都有一个想法如何解决这个问题?
这是一种获取数据框架的方法:
library(rjson)
文件< - fromJSON(file =bevingenbag.json,method ='C')
dat< - do.call(rbind,lapply(document $ features,
function(x)data.frame(x $ properties)))
编辑:如何用 NA替换空值
:
dat $ baglocatie [dat $ baglocatie ==]< - NA
结果:
head(dat)
gid yymmdd lat lon mag depth knmilocatie baglocatie tijd
1 1496600 19861226 52.992 6.548 2.8 1.0 Assen Assen 74751
2 1496601 19871214 52.928 6.552 2.5 1.5 Hooghalen Hooghalen 204951
3 1496602 19891201 52.529 4.9 71 2.7 1.2 Purmerend Kwadijk 200914
4 1496603 19910215 52.771 6.914 2.2 3.0 Emmen Emmen 21116
5 1496604 19910425 52.952 6.575 2.6 3.0 Geelbroek Ekehaar 102631
6 1496605 19910808 52.965 6.573 2.7 3.0 Eleveld Assen 40114
I want to convert a json-file into a dataframe in R. With the following code:
link <- 'https://www.dropbox.com/s/ckfn1fpkcix1ccu/bevingenbag.json'
document <- fromJSON(file = link, method = 'C')
bev <- do.call("cbind", document)
i'm getting this:
type features
1 FeatureCollection list(type = "Feature", geometry = list(type = "Point", coordinates = c(6.54800000288927, 52.9920000044505)), properties = list(gid = "1496600", yymmdd = "19861226", lat = "52.992", lon = "6.548", mag = "2.8", depth = "1.0", knmilocatie = "Assen", baglocatie = "Assen", tijd = "74751"))
which is the first row of a matrix. All the other rows have the same structure. I'm interested in the properties = list(gid = "1496600", yymmdd = "19861226", lat = "52.992", lon = "6.548", mag = "2.8", depth = "1.0", knmilocatie = "Assen", baglocatie = "Assen", tijd = "74751")
part, which should be converted into a dataframe with the columns gid, yymmdd, lat, lon, mag, depth, knmilocatie, baglocatie, tijd
.
I searched for and tryed several solutions but none of them worked. I used the rjson package for this. I also tryed the RJSONIO & jsonlite package, but was unable to extract the desired information.
Anyone an idea how to solve this problem?
Here's a way to obtain the data frame:
library(rjson)
document <- fromJSON(file = "bevingenbag.json", method = 'C')
dat <- do.call(rbind, lapply(document$features,
function(x) data.frame(x$properties)))
Edit: How to replace empty values with NA
:
dat$baglocatie[dat$baglocatie == ""] <- NA
The result:
head(dat)
gid yymmdd lat lon mag depth knmilocatie baglocatie tijd
1 1496600 19861226 52.992 6.548 2.8 1.0 Assen Assen 74751
2 1496601 19871214 52.928 6.552 2.5 1.5 Hooghalen Hooghalen 204951
3 1496602 19891201 52.529 4.971 2.7 1.2 Purmerend Kwadijk 200914
4 1496603 19910215 52.771 6.914 2.2 3.0 Emmen Emmen 21116
5 1496604 19910425 52.952 6.575 2.6 3.0 Geelbroek Ekehaar 102631
6 1496605 19910808 52.965 6.573 2.7 3.0 Eleveld Assen 40114
这篇关于无法将JSON转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!