简体   繁体   中英

Trouble extracting data from Json file into an R data frame

I have a JSON file that I want to import into R.

Please see the screenshots for reference屏幕截图 1.

As you can see the data is nested in different subfiles.

The problem is that when extracting the data from the JSON file I get a mixed data.table with information in rows that don't belong.

图片在这里

here is my code:

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))

I also get this message error

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

Here is the sample data: 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)),

Is it clear? please, let me know if I can improve the question

What you are downloading is called an environment, a list of lists and Data frames. Not a simple list. In RStudio view the environment. Try to find the elements you are interested in. Look to the extreme right for a small square. Click on it. This will create a reference to the items you are interested in. Use that to create a list or Dataframe or whatever you need. Unlist only if you have to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM