I have some data in JSON I am trying to use in R. My problem is I cannot get the data in the right format.
require(RJSONIO)
json <- "[{\"ID\":\"id1\",\"VALUE\":\"15\"},{\"ID\":\"id2\",\"VALUE\":\"10\"}]"
example <- fromJSON(json)
example <- do.call(rbind,example)
example <- as.data.frame(example,stringsAsFactors=FALSE)
> example
ID VALUE
1 id1 15
2 id2 10
This gets close, but I cannot get the numeric column to convert to numeric. I know I can convert columns manually, but I thought data.frame
or as.data.frame
scanned the data and made the most appropriate class definitions. Clearly I misunderstood. I am reading in numerous tables - all very different - and I need to have the numeric data treated as such when it's numeric.
Ultimately I am looking to get data tables with numeric columns when the data is numeric.
read.table
uses type.convert
to convert data to the appropriate type. You could do the same as a cleaning step after reading in the JSON data.
sapply(example,class)
# ID VALUE
# "character" "character"
example[] <- lapply(example, type.convert, as.is = TRUE)
sapply(example, class)
# ID VALUE
# "character" "integer"