Strategies for formatting JSON output from R

前端 未结 5 1202
青春惊慌失措
青春惊慌失措 2020-12-13 20:12

I\'m trying to figure out the best way of producing a JSON file from R. I have the following dataframe tmp in R.

> tmp
  gender         


        
相关标签:
5条回答
  • 2020-12-13 20:27

    Building further on Andrie and Richie's ideas, use alply instead of apply to avoid converting numbers to characters:

    library(RJSONIO)
    library(plyr)
    modified <- list(
      traits = colnames(tmp),
      values = unname(alply(tmp, 1, identity))
    )
    cat(toJSON(modified))
    

    plyr's alply is similar to apply but returns a list automatically; whereas without the more complicated function inside Richie Cotton's answer, apply would return a vector or array. And those extra steps, including t, mean that if your dataset has any non-numeric columns, the numbers will get converted to strings. So use of alply avoids that concern.

    For example, take your tmp dataset and add

    tmp$grade <- c("A","B","C","D","E","F")
    

    Then compare this code (with alply) vs the other example (with apply).

    0 讨论(0)
  • 2020-12-13 20:39

    Building upon Andrie's idea with apply, you can get exactly what you want by modifying the tmp variable before calling toJSON.

    library(RJSONIO)
    modified <- list(
      traits = colnames(tmp),
      values = unname(apply(tmp, 1, function(x) as.data.frame(t(x))))
    )
    cat(toJSON(modified))
    
    0 讨论(0)
  • 2020-12-13 20:40

    Using the package jsonlite:

    > jsonlite::toJSON(list(traits = names(tmp), values = tmp), pretty = TRUE)
    {
      "traits": ["gender", "age", "welcoming", "proud", "tidy", "unique"],
      "values": [
        {
          "gender": 1,
          "age": 30,
          "welcoming": 4,
          "proud": 4,
          "tidy": 4,
          "unique": 4
        },
        {
          "gender": 2,
          "age": 34,
          "welcoming": 4,
          "proud": 2,
          "tidy": 4,
          "unique": 4
        },
        {
          "gender": 1,
          "age": 34,
          "welcoming": 5,
          "proud": 3,
          "tidy": 4,
          "unique": 5
        },
        {
          "gender": 2,
          "age": 33,
          "welcoming": 2,
          "proud": 3,
          "tidy": 2,
          "unique": 4
        },
        {
          "gender": 2,
          "age": 28,
          "welcoming": 4,
          "proud": 3,
          "tidy": 4,
          "unique": 4
        },
        {
          "gender": 2,
          "age": 26,
          "welcoming": 3,
          "proud": 2,
          "tidy": 4,
          "unique": 3
        }
      ]
    } 
    
    0 讨论(0)
  • 2020-12-13 20:40

    Another option is to use the split to split your data.frame with N rows into N data.frames with 1 row.

    library(RJSONIO)
    modified <- list(
       traits = colnames(tmp),
       values = split(tmp, seq_len(nrow(tmp)))
    )
    cat(toJSON(modified))
    
    0 讨论(0)
  • 2020-12-13 20:43

    It seems to me you can do this by sending each row of your data.frame to JSON with the appropriate apply statement.

    For a single row:

    library(RJSONIO)
    
    > x <- toJSON(tmp[1, ])
    > cat(x)
    {
     "gender": 1,
    "age":     30,
    "welcoming": 4,
    "proud": 4,
    "tidy": 4,
    "unique": 4 
    }
    

    The entire data.frame:

    x <- apply(tmp, 1, toJSON)
    cat(x)
    {
     "gender": 1,
    "age":     30,
    "welcoming": 4,
    "proud": 4,
    "tidy": 4,
    "unique": 4 
    } {
    
    ...
    
    } {
     "gender": 2,
    "age":     26,
    "welcoming": 3,
    "proud": 2,
    "tidy": 4,
    "unique": 3 
    }
    
    0 讨论(0)
提交回复
热议问题