我正在尝试将json文件加载到r中的data.frame中.我对jsonlite包中的fromJSON函数运气不错 - 但我得到了嵌套列表,并且不确定如何将输入展平为二维data.frame.Jsonlite以data.frame的形式读取文件,但在一些变量中留下嵌套列表.
有人在使用嵌套列表读入时将任何JSON文件加载到data.frame有任何提示.
#*#*#*#*#*#*#*#*#*##*#*#*#*#*#*#*#*#*# HERE IS MY EXAMPLE #*#*#*#*#*#*#*#*#*##*#*#*#*#*#*#*#*#*#
# loads the packages
library("httr")
library( "jsonlite")
# downloads an example file
providers <- fromJSON( "http://fm.formularynavigator.com/jsonFiles/publish/11/47/providers.json" , simplifyDataFrame=TRUE )
# the flatten function breaks the name variable into three vars ( first name, middle name, last name)
providers <- flatten( providers )
# but many of the columns are still lists:
sapply( providers , class)
# Some of these lists have a single level
head( providers$facility_type )
# Some have …Run Code Online (Sandbox Code Playgroud) 考虑以下列表:
x <- list("a" = list("b", "c"),
"d" = list("e", "f" = list("g", "h")),
"i" = list("j", "k" = list("l" = list("m", "n" = list("o", "p")))))
Run Code Online (Sandbox Code Playgroud)
值得一提的是:
鉴于x,我的目标是输出数据框:
y <- data.frame(
main_level = c(rep("a", 2), rep("d", 3), rep("i", 4)),
level1 = c("b", "c", "e", rep("f", 2), "j", rep("k", 3)),
level2 = c(NA, NA, NA, "g", "h", NA, "l", "l", "l"),
level3 = c(NA, NA, NA, NA, NA, NA, "m", "n", "n"),
level4 = c(NA, NA, …Run Code Online (Sandbox Code Playgroud) I have a list of list as given below. ANd I want to convert it into dataframe in the desired format.
myList:
[[1]]
NULL
[[2]]
[[2]]$`file`
[1] "ABC"
[[2]]$New
[1] 21
[[2]]$Old
[1] 42
[[3]]
[[3]]$`file`
[1] "CDF"
[[3]]$NEW
[1] 206
[[3]]$Old
[1] 84
Run Code Online (Sandbox Code Playgroud)
And I want to convert this list of list object to dataframe in the desired format:
file New Old
ABC 21 42
CDF 206 84
Run Code Online (Sandbox Code Playgroud)
Thanks in advance!