Is it possible to write a table to a file in JSON format in R?

前端 未结 3 1328
滥情空心
滥情空心 2021-02-13 16:35

I\'m making word frequency tables with R and the preferred output format would be a JSON file. sth like { \"word\" : \"dog\", \"frequency\" : 12 } Is there any

相关标签:
3条回答
  • 2021-02-13 16:58

    RJSONIO is a package "that allows conversion to and from data in Javascript object notation (JSON) format". You can use it to export your object as a JSON file.

    library(RJSONIO)    
    writeLines(toJSON(anobject), "afile.JSON")
    
    0 讨论(0)
  • 2021-02-13 17:03
    set.seed(1)
    ( tbl <- table(round(runif(100, 1, 5))) )
    
    ## 1  2  3  4  5 
    ## 9 24 30 23 14 
    
    library(rjson)
    sink("json.txt")
    cat(toJSON(tbl))
    sink()
    
    file.show("json.txt")
    ## {"1":9,"2":24,"3":30,"4":23,"5":14}
    

    or even better:

    set.seed(1)
    ( tab <- table(letters[round(runif(100, 1, 26))]) )
    
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
    1 2 4 3 2 5 4 3 5 3 9 4 7 2 2 2 5 5 5 6 5 3 7 3 2 1 
    
    sink("lets.txt")
    cat(toJSON(tab))
    sink()
    file.show("lets.txt")
    ## {"a":1,"b":2,"c":4,"d":3,"e":2,"f":5,"g":4,"h":3,"i":5,"j":3,"k":9,"l":4,"m":7,"n":2,"o":2,"p":2,"q":5,"r":5,"s":5,"t":6,"u":5,"v":3,"w":7,"x":3,"y":2,"z":1}
    

    Then validate it with http://www.jsonlint.com/ to get pretty formatting. If you have multidimensional table, you'll have to work it out a bit...

    EDIT:

    Oh, now I see, you want the dataset characteristics sink-ed to a JSON file. No problem, just give us a sample data, and I'll work on a code a bit. Practically, you need to carry out the data into desirable format, hence convert it to JSON. list should suffice. Give me a sec, I'll update my answer.

    EDIT #2: Well, time is relative... it's a common knowledge... Here you go:

    ( dtf <- structure(list(word = structure(1:3, .Label = c("cat", "dog", 
    "mouse"), class = "factor"), frequency = c(12, 32, 18)), .Names = c("word", 
    "frequency"), row.names = c(NA, -3L), class = "data.frame") )
    
    ##   word frequency
    ## 1   cat        12
    ## 2   dog        32
    ## 3 mouse        18
    

    If dtf is a simple data frame, yes, data.frame, if it's not, coerce it! Long story short, you can do:

    toJSON(as.data.frame(t(dtf)))
    ## [1] "{\"V1\":{\"word\":\"cat\",\"frequency\":\"12\"},\"V2\":{\"word\":\"dog\",\"frequency\":\"32\"},\"V3\":{\"word\":\"mouse\",\"frequency\":\"18\"}}"
    

    I though I'll need some melt with this one, but simple t did the trick. Now, you only need to deal with column names after transposing the data.frame. t coerces data.frames to matrix, so you need to convert it back to data.frame. I used as.data.frame, but you can also use toJSON(data.frame(t(dtf))) - you'll get X instead of V as a variable name. Alternatively, you can use regexp to clean the JSON file (if needed), but it's a lousy practice, try to work it out by preparing the data.frame.

    I hope this helped a bit...

    0 讨论(0)
  • 2021-02-13 17:09

    These days I would typically use the jsonlite package.

    library("jsonlite")
    toJSON(mydatatable, pretty = TRUE)
    

    This turns the data table into a JSON array of key/value pair objects directly.

    0 讨论(0)
提交回复
热议问题