3

I have a data frame in R with two columns, for example:

df = data.frame(x=c(1, 2, 3, 4), y=c(5, 6, 7, 8))

I need to write an R script to convert this data.frame to a JSON array of arrays, like this:

[[1, 5],[2, 6],[3, 7],[4, 8]]

How do I convert my data frame to a JSON array of rows in R?

1 Answer 1

5

We can use toJSON from library(jsonlite)

library(jsonlite)
toJSON(setNames(df, NULL))
#[[1,5],[2,6],[3,7],[4,8]] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.