Strategies for formatting JSON output from R

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

提交回复
热议问题