无法使用R将多级JSON加载到数据帧中

时间:2018-09-26 00:04:25

标签: r json dataframe jsonlite rjsonio

我无法将此嵌套JSON加载到数据框中。

require(RJSONIO)
sample_json2 <- '[{ "_id" : { "$oid" : "50b59cd75bed76f46522c34e" },         "student_id" : 0, "class_id" : 2, "scores" : [ { "type" : "exam", "score" : 57.92947112575566 }, { "type" : "quiz", "score" : 21.24542588206755 }, { "type" : "homework", "score" : 68.19567810587429 }, { "type" : "homework", "score" : 67.95019716560351 }, { "type" : "homework", "score" : 18.81037253352722 } ] }

,{ "_id" : { "$oid" : "50b59cd75bed76f46522c34f" }, "student_id" : 0,      "class_id" : 28, "scores" : [ { "type" : "exam", "score" : 39.17749400402234 }, { "type" : "quiz", "score" : 78.44172815491468 }, { "type" : "homework", "score" : 20.81782269075502 }, { "type" : "homework", "score" : 70.44520452408949 }, { "type" : "homework", "score" : 50.66616327819226 }, { "type" : "homework", "score" : 53.84983118363991 } ] }

,{ "_id" : { "$oid" : "50b59cd75bed76f46522c350" }, "student_id" : 0,    "class_id" : 5, "scores" : [ { "type" : "exam", "score" : 88.22950674232497 }, { "type" : "quiz", "score" : 79.28962650427184 }, { "type" : "homework", "score" : 18.66254946562674 }, { "type" : "homework", "score" : 40.28154176513361 }, { "type" : "homework", "score" : 1.23735944117882 }, { "type" : "homework", "score" : 88.96101200683958 } ] }

,{ "_id" : { "$oid" : "50b59cd75bed76f46522c351" }, "student_id" : 0, "class_id" : 16, "scores" : [ { "type" : "exam", "score" : 59.1805667559299 }, { "type" : "quiz", "score" : 47.58960202938239 }, { "type" : "homework", "score" : 6.48470951607214 }, { "type" : "homework", "score" : 68.33519637418685 }, { "type" : "homework", "score" : 78.53068038180965 } ] }

,{ "_id" : { "$oid" : "50b59cd75bed76f46522c352" }, "student_id" : 0, "class_id" : 24, "scores" : [ { "type" : "exam", "score" : 4.444435759027499 }, { "type" : "quiz", "score" : 28.63057857803885 }, { "type" : "homework", "score" : 86.79352850434199 }, { "type" : "homework", "score" : 83.9164548767836 } ] }

,{ "_id" : { "$oid" : "50b59cd75bed76f46522c353" }, "student_id" : 0, "class_id" : 30, "scores" : [ { "type" : "exam", "score" : 14.34345947841966 }, { "type" : "quiz", "score" : 47.65945482174327 }, { "type" : "homework", "score" : 83.42772189120254 }, { "type" : "homework", "score" : 49.86812935368258 }, { "type" : "homework", "score" : 39.85525554437086 } ] }

,{ "_id" : { "$oid" : "50b59cd75bed76f46522c354" }, "student_id" : 0, "class_id" : 7, "scores" : [ { "type" : "exam", "score" : 18.20492211025179 }, { "type" : "quiz", "score" : 60.4769945611789 }, { "type" : "homework", "score" : 75.62999921143397 }, { "type" : "homework", "score" : 72.41228797373115 }, { "type" : "homework", "score" : 74.06744381708968 } ] }]'

我尝试使用 json_file2 <- fromJSON(sample_json2, flatten = TRUE)用于jsonlite程序包,但它仍具有得分作为串联列表。

我后来尝试套用,但是数据帧中的输出不是所需的格式。它的列表对任何读者都没有意义。请注意,我是R的新手,所以只关注在线博客并尝试获取输出。下面是我使用lapply的代码。

json_file2 <- fromJSON(sample_json2)
df <- lapply(json_file2, function(play){
    data.frame(matrix(unlist(play, recursive = TRUE), byrow = FALSE))
})

我想要的输出格式将是5行,每种乐谱类型排在一行中。如果我可以保留列名称中的所有列标签,我也希望。例如:_id。$ oid,student_id,class_id,类型,Scores.type,scores.score。但是下面的格式也可以正常工作。

$oid,                  student_id   class_id   type     score
50b59cd75bed76f46522c35    0            7      exam     75.62
50b59cd75bed76f46522c354   0            7      quiz     59.62
50b59cd75bed76f46522c354   0            7    homework   59.62
50b59cd75bed76f46522c354   0            7    Homework   59.62

有人可以帮助我解决此问题吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

您好,欢迎光临!

我同意@SymbolixAU的观点,它更容易格式化mongo查询,但是,在无法实现的情况下,我尝试了一下。

请注意,这段代码只是一个非常丑陋和肮脏的解决方案,我也正在学习R的学习者,但它应该可以帮助您入门。我在这里使用了tidyverse,因为您没有指定它是否必须使用基数R。如果您不熟悉它,请转到here

library(tidyverse)
json_file <- RJSONIO::fromJSON(sample_json2, flatten = TRUE, simplify = TRUE)

df <- json_file %>%
    unlist(recursive = F) %>%
    matrix(ncol = 4, byrow = T) %>% # Converts the data to a matrix
    as_data_frame %>% # Converts to tibble, for easier handling
    mutate(V4 = map(V4, ~ data.frame(
         matrix(unlist(.), ncol = 2, byrow = T), stringsAsFactors = FALSE)
    )) %>%
    unnest(V1, V2, V3) %>%
    unnest(V4) %>% # Can't do both on a single unnest
    mutate(X2 = as.numeric(X2), V2 = as.integer(V2), V3 = as.integer(V3)) %>%
    rename(oid = V1, student_id = V2, class_id = V3, type = X1, score = X2)

您将得到以下结果:

> df
# A tibble: 36 x 5
   oid                      student_id class_id type     score
   <chr>                         <int>    <int> <chr>    <dbl>
 1 50b59cd75bed76f46522c34e          0        2 exam      57.9
 2 50b59cd75bed76f46522c34e          0        2 quiz      21.2
 3 50b59cd75bed76f46522c34e          0        2 homework  68.2
 4 50b59cd75bed76f46522c34e          0        2 homework  68.0
 5 50b59cd75bed76f46522c34e          0        2 homework  18.8
 6 50b59cd75bed76f46522c34f          0       28 exam      39.2
 7 50b59cd75bed76f46522c34f          0       28 quiz      78.4
 8 50b59cd75bed76f46522c34f          0       28 homework  20.8
 9 50b59cd75bed76f46522c34f          0       28 homework  70.4
10 50b59cd75bed76f46522c34f          0       28 homework  50.7
# ... with 26 more rows

注意:我有一个非常类似的问题,这就是为什么我共享代码的原因,但是请注意,SO标准不鼓励这样做...