首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中将JSON文件转换为data.frame

在R中将JSON文件转换为data.frame
EN

Stack Overflow用户
提问于 2017-02-09 01:03:35
回答 2查看 2.1K关注 0票数 1

我有一个文件,其中包含JSON格式的事件数据(用户与app交互的点击数据),我需要将其转换为数据帧。我使用了这个:

代码语言:javascript
复制
require(RJSONIO)
result <- fromJSON('events_data.json',nullValue = NA)
result <- do.call(rbind,lapply(result,data.frame,stringsAsFactors=FALSE))

它会生成这个结果

代码语言:javascript
复制
summary(result)
                  Length Class  Mode     
_id               1      -none- character
session           2      -none- list     
metrics           0      -none- list     
arrival_timestamp 1      -none- character
event_type        1      -none- character
event_timestamp   1      -none- character
event_version     1      -none- character
application       7      -none- list     
client            2      -none- character
device            4      -none- list     
attributes        3      -none- character

当我试图将这个列表转换成数据框时,我得到了一个错误

代码语言:javascript
复制
Error in data.frame(locale = c("US", "en_US", "en"), platform = c("5.1.1",  : 
  arguments imply differing number of rows: 3, 2, 1

这里是数据文件click here(仅供参考)。谁能帮帮我。JSON文件容器

代码语言:javascript
复制
{"_id":{"$oid":"57a30ce268fd0809ec4d194f"},"session":{"start_timestamp":{"$numberLong":"1470183490481"},"session_id":"def5faa9-20160803-001810481"},"metrics":{},"arrival_timestamp":{"$numberLong":"1470183523054"},"event_type":"OfferViewed","event_timestamp":{"$numberLong":"1470183505399"},"event_version":"3.0","application":{"package_name":"com.think.vito","title":"Vito","version_code":"5","app_id":"7ffa58dab3c646cea642e961ff8a8070","cognito_identity_pool_id":"us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74","version_name":"2.0.0.0","sdk":{"version":"2.2.2","name":"aws-sdk-android"}},"client":{"cognito_id":"us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37","client_id":"ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"},"device":{"locale":{"country":"US","code":"en_US","language":"en"},"platform":{"version":"5.1.1","name":"ANDROID"},"make":"YU","model":"AO5510"},"attributes":{"Category":"120000","CustomerID":"4078","OfferID":"45436"}}
{"_id":{"$oid":"57a30ce268fd0809ec4d1950"},"session":{"start_timestamp":{"$numberLong":"1470183490481"},"session_id":"def5faa9-20160803-001810481"},"metrics":{},"arrival_timestamp":{"$numberLong":"1470183523054"},"event_type":"ContextMenuItemSelected","event_timestamp":{"$numberLong":"1470183500206"},"event_version":"3.0","application":{"package_name":"com.think.vito","title":"Vito","version_code":"5","app_id":"7ffa58dab3c646cea642e961ff8a8070","cognito_identity_pool_id":"us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74","version_name":"2.0.0.0","sdk":{"version":"2.2.2","name":"aws-sdk-android"}},"client":{"cognito_id":"us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37","client_id":"ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"},"device":{"locale":{"country":"US","code":"en_US","language":"en"},"platform":{"version":"5.1.1","name":"ANDROID"},"make":"YU","model":"AO5510"},"attributes":{"MenuItem":"OfferList","CustomerID":"4078"}}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-10 03:16:13

我得到了答案,实际上我没有读完整个json文件。

代码语言:javascript
复制
library(rjson)
library(plyr)

json_file <- "events_data.json"

con <- file(json_file, "r")
input <- readLines(con,-1L)
close(con)

json_file2 <- ldply(lapply(input, function(x) t(unlist(fromJSON(x)))))
票数 0
EN

Stack Overflow用户

发布于 2017-02-09 01:35:17

我能够很好地下载和读取你的数据。使用ndjson库。看看这个。如果它能帮你找到你想要的.

df<-ndjson::stream_in('events_data.json')

祝好运。干杯,NF

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42119323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档