Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling NaN when using fromJSON in R

Tags:

json

r

rjsonio

I'm trying to use the fromJSON function in R to read in a JSON file that was given to me, however this file has NaN in it and I can't read it in properly.

This is the error I get:

Error in feed_push_parser(buf) : 
  lexical error: invalid char in json text.

Anyone know how to read NaN values when reading in a json file into R?

like image 797
user2020282 Avatar asked Aug 12 '15 02:08

user2020282


People also ask

What does fromJSON do in R?

The fromJSON() function reads the content in JSON format and de-serializes it into R objects. JSON content is produced of logical values, integers, real numbers, strings, arrays using the key: value pairs. The toJSON and fromJSON methods use class-based mapping.

What is Jsonlite?

jsonlite: A Simple and Robust JSON Parser and Generator for R. A reasonably fast JSON parser and generator, optimized for statistical data and the web. Offers simple, flexible tools for working with JSON in R, and is particularly powerful for building pipelines and interacting with a web API.


1 Answers

As referenced in the comments, RJSONIO can handle NaN. By default, the NaN values will be excluded. If you want to include the NaN values you can set the NaN values to NA via nullValue.

Example Code - Replacing NaN with NA

library(RJSONIO)

json_imported <- fromJSON(content,nullValue=NA)

The value for 'content 'is the JSON content. Via RJSONIO documentation, "This can be the name of a file or the content itself as a character string. We will add support for connections in the near future."

like image 79
GameChanger Avatar answered Oct 15 '22 05:10

GameChanger