Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:25:50+00:00 2026-05-16T14:25:50+00:00

I’m making word frequency tables with R and the preferred output format would be

  • 0

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 way to save the table directly into this format? I’ve been using the write.csv() function and convert the output into JSON but this is very complicated and time consuming.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:25:50+00:00Added an answer on May 16, 2026 at 2:25 pm
    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.