直接从JSON文件获取数据帧? [英] Getting dataframe directly from JSON-file?
问题描述
首先,让我感谢所有为Stackoverflow和R做出贡献的人!我是那些不太擅长编程的R用户之一,但是勇敢地尝试将其用于工作,因此下面的问题可能很简单...
First, let me thank everybody who contributes to Stackoverflow and R! I'm one of those R-users who is not so good at programming, but bravely try to use it for work, so the issue below is probably trivial...
这是问题所在.我需要将JSON格式的文件导入R:
Here's the problem. I need to import files in JSON-format to R:
# library(plyr)
# library(RJSONIO)
# lstJson <- fromJSON("JSON_test.json") #This is the file I read
# dput(lstJson) #What I did to get the txtJson below, for the benefit of testing.
txtJson <- structure(list(version = "1.1", result = structure(list(warnings = structure(list(), class = "AsIs"), fields = list(structure(list(info = "", rpl = 15, name = "time", type = "timeperiod"), .Names = c("info", "rpl", "name", "type")), structure(list(info = "", name = "object", type = "string"), .Names = c("info", "name", "type")), structure(list(info = "Counter1", name = "Counter1", type = "int"), .Names = c("info", "name", "type")), structure(list( info = "Counter2", name = "Counter2", type = "int"), .Names = c("info", "name", "type"))), timeout = 180, name = NULL, data = list( list(list("2011-05-01 17:00", NULL), list("Total", NULL), list(8051, NULL), list(44, NULL)), list(list("2011-05-01 17:15", NULL), list("Total", NULL), list(8362, NULL), list( 66, NULL))), type = "AbcDataSet"), .Names = c("warnings", "fields", "timeout", "name", "data", "type"))), .Names = c("version", "result"))
dfJson <- ldply(txtJson, data.frame)
我需要的是与此相似的数据框:
What I need is a data frame similar to this:
time object Counter1 Counter2
2011-05-01 17:00 Total 8051 44
2011-05-01 17:15 Total 8362 66
但是我得到了
"Error in data.frame("2011-05-01 17:00", NULL, check.names = FALSE, stringsAsFactors = TRUE) :
arguments imply differing number of rows: 1, 0"
如果我使用lstJson,也会遇到相同的错误.
I get the same error if I use the lstJson.
我不确定RJSONIO
是否应该足够聪明"来解析此类文件,或者我是否必须手动读取文件的第一行,设置列类型等.原因是我不使用CSV的原因是我想自动"获取日期格式等的日期.
I'm not sure if RJSONIO
is supposed to be "smart enough" to parse files like this, or if I have to manually read the first line of the file, set column-types etc. The reason I'm not using CSV is that I want to "automatically" get dates in date-format, etc.
谢谢, /克里斯
推荐答案
查看txtJson的结构,您会发现所有有用的位都在txtJson $ result $ data中:
Looking at the structure of txtJson you see that all of the useful bits are in txtJson$result$data:
> sapply( txtJson$result$data, unlist )
[,1] [,2]
[1,] "2011-05-01 17:00" "2011-05-01 17:15"
[2,] "Total" "Total"
[3,] "8051" "8362"
[4,] "44" "66"
> t(sapply( txtJson$result$data, unlist ))
[,1] [,2] [,3] [,4]
[1,] "2011-05-01 17:00" "Total" "8051" "44"
[2,] "2011-05-01 17:15" "Total" "8362" "66"
> as.data.frame(t(sapply( txtJson$result$data, unlist )) )
V1 V2 V3 V4
1 2011-05-01 17:00 Total 8051 44
2 2011-05-01 17:15 Total 8362 66
在将它们作为未列出的向量进行获取然后传递给"as.data.frame"的过程中,它们现在都是因子"类,因此可能需要付出更多的努力来重新分类()这些值.您可以改用:
In the process of gettting these as unlisted vectors and then passing to 'as.data.frame' they are now all class 'factor', so there is probably additional effort to re-class() these values. You can instead use:
data.frame(t(sapply( txtJson$result$data, unlist )) ,stringsAsFactors=FALSE)
它们都是字符"
对于导入CSV文件,read.table()的colClasses参数将接受"POSIXlt"或"POSIXct"作为已知类型.我相信规则是必须有一个as. _ 方法可用.这是一个最小的示例:
As far as importing CSV files, read.table()'s colClasses argument will accept "POSIXlt" or "POSIXct" as known types. The rule I believe is that there must an as._ method available. Here's a minimal example:
> read.table(textConnection("2011-05-01 17:00"), sep=",", colClasses="POSIXct")
V1
1 2011-05-01 17:00:00
这篇关于直接从JSON文件获取数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!