【问题标题】:Trouble extracting data from Json file into an R data frame无法将 Json 文件中的数据提取到 R 数据框中
【发布时间】:2020-10-14 18:07:52
【问题描述】:

我有一个 JSON 文件要导入到 R 中。

请查看截图以供参考。

如您所见,数据嵌套在不同的子文件中。

问题是,从 JSON 文件中提取数据时,我得到一个混合数据表,其中包含不属于的行中的信息。

这是我的代码:

data <- fromJSON("mayo-7996c-export.json")

json_file <- lapply(data, function(x) {
    x[sapply(x, is.null)] <- NA
    unlist(x)
})

df<-as.data.frame(do.call("cbind", json_file))

我也收到此消息错误

Warning message:
In cbind(channels = c(`1538775970275.users.NqHHgP8hUQOYTkqSCu1WalNEZbC3` = "0",  :
  number of rows of result is not a multiple of vector length (arg 1)

这里是示例数据:dput(head(data))

-123.324442142736)), `1580965620278` = list(g = "c25pprxwvd", 
        l = c(46.272837, -119.202105)), `1580967003376` = list(
        g = "c25pprxwvd", l = c(46.272837, -119.202105)), 
    `1580967240856` = list(g = "c25pprxwvd", l = c(46.272837, 
    -119.202105)), `1581011053106` = list(g = "c23p0u442q", 
        l = c(47.6591127532245, -122.308026642539)), `1581012747200` = list(
        g = "c23nb5j99b", l = c(47.6095141701478, -122.336114324036
        )), `1581016826907` = list(g = "c23p0g8c6n", l = c(47.6561938506064, 
    -122.30966473941)), `1581024747917` = list(.priority = "c23p0g84hv", 
        g = "c23p0g84hv", l = c(47.6563152, -122.3105839)), 
    `1581033551685` = list(g = "c23nb4yp3k", l = c(47.6091501116343, 
    -122.335454263027)), `1581038799349` = list(g = "c23nb5sdz1", 
        l = c(47.6124801044895, -122.337269291542)), `1581100481406` = list(
        g = "c23p0ud1jp", l = c(47.6616506030942, -122.307822615892
        )), `1581101479449` = list(g = "c23p0g27h7", l = c(47.6551056142281, 
    -122.310256892219)), `1581105811818` = list(g = "c25pprxwvd", 
        l = c(46.272837, -119.202105)), `1581105931342` = list(
        g = "c25pprxwvd", l = c(46.272837, -119.202105)), 
    `1581106432937` = list(g = "c23p22ptqm", l = c(47.6815923461579, 
    -122.322190813855)), `1581106878382` = list(g = "c23p0g6479", 
        l = c(47.6549642673206, -122.307888697947)), `1581109943285` = list(
        g = "c25pprxwvd", l = c(46.272837, -119.202105)), 
    `1581110531443` = list(g = "c23p2d7315", l = c(47.6932163397294, 
    -122.317267041767)), `1581110855812` = list(g = "c25pprxt4h", 
        l = c(46.2725522962073, -119.20225589975)), `1581126978120` = list(
        g = "c25pprxwvd", l = c(46.272837, -119.202105)), 
    `1581129965670` = list(g = "c25pprxwvd", l = c(46.272837, 
    -119.202105)), `1581277963529` = list(g = "dnnrzbbc18", 
        l = c(35.1165989488931, -80.8692647315726)), `1581278648747` = list(
        g = "dnnrzbbc1b", l = c(35.1165966290032, -80.8692553118908
        )), `1581313976188` = list(g = "c25pprxwvd", l = c(46.272837, 
    -119.202105)), `1581316382332` = list(g = "c25pprxwvd", 
        l = c(46.272837, -119.202105)), `1581317778200` = list(
        g = "c25pprxwvd", l = c(46.272837, -119.202105)), 
    `1581320237207` = list(g = "c25pprxwvd", l = c(46.272837, 
    -119.202105)), `1581320413918` = list(g = "c25pprxwvd", 
        l = c(46.272837, -119.202105)), `1581331518497` = list(
        g = "u281z48n1u", l = c(48.1349044099976, 11.5576956596722
        )), `1581358349287` = list(g = "c23nb5j9z3", l = c(47.6095597765124, 
    -122.335877094208)), `1581359435922` = list(g = "c23nb5j9se", 
        l = c(47.6095308306119, -122.336001621712)), `1581369083560` = list(
        .priority = "9zy4ry5hjt", g = "9zy4ry5hjt", l = c(44.0229307, 
        -92.467567)),

清楚吗?请告诉我是否可以改进问题

【问题讨论】:

  • 为了改进您的问题,您可以包含dput(head(data)) 的输出 - 而不是包含屏幕截图。它将帮助人们重现您的问题 - 人们无法从屏幕截图中复制和粘贴代码。谢谢。
  • 伟大的推荐!刚刚添加了代码示例。谢谢

标签: r json


【解决方案1】:

您正在下载的内容称为环境、列表列表和数据框。不是一个简单的清单。在 RStudio 查看环境。尝试找到您感兴趣的元素。向最右边寻找一个小方块。点击它。这将创建对您感兴趣的项目的引用。使用它来创建列表或数据框或您需要的任何内容。仅在必要时不列出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2021-10-25
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    相关资源
    最近更新 更多