Strategies for formatting JSON output from R

前端 未结 5 1203
青春惊慌失措
青春惊慌失措 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: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 
    }
    

提交回复
热议问题