我搜索了论坛并找不到类似的问题,所以如果我可能错过了,我深表歉意。
简单地说,我得到了一个JSON
文件,我想创建它。所以我认为一个简单的方法是使用包中的fromJSON
函数解析文件jsonlite
并查看 R 对象的结构,然后尝试JSON
通过首先创建 R 对象并使用函数来创建文件toJSON
。
JSON
我得到的文件如下:
[[{"name":"Wine"},{"data":[{"rating":"23.45","month":8,"year":2012,"day":8},{"rating":"23.66","month":8,"year":2012,"day":9},{"rating":"24.75","month":8,"year":2013,"day":9},{"rating":"24.97","month":8,"year":2013,"day":12}]}]]
用函数解析上述JSON
文件fromJSON
会得到以下对象:
str(object)
List of 1
$ :'data.frame': 2 obs. of 2 variables:
..$ name: chr [1:2] "Wine" NA
..$ data:List of 2
.. ..$ : NULL
.. ..$ :'data.frame': 248 obs. of 4 variables:
.. .. ..$ rating: chr [1:248] "23.45" "23.66" "23.59" "23.48" ...
.. .. ..$ month : int [1:248] 8 8 8 8 8 8 8 8 8 8 ...
.. .. ..$ year : int [1:248] 2012 2012 2012 2012 2012 2012 2012 2012 ...
.. .. ..$ day : int [1:248] 8 9 10 13 14 15 16 17 20 21 ...
创建上述对象。值是随机的
name = c("Wine", NA)
data = list(NULL, data.frame(rating = as.character(c(1:248)), month = as.integer(rep(12, 248)), year = as.integer(rep(1940, 248)), day = as.integer(rep(31, 248)), stringsAsFactors = FALSE))
result = data.frame(name, data)
最后一行给出了错误:
Error in data.frame(NULL, list(rating = c("1", "2", "3", "4", "5", "6", : arguments imply differing number of rows: 0, 248
有人有想法吗?