How to read a column(json format) from a data frame in R?




I have a csv file which I imported in R in the data frame ‘train’. It has a column which contains data in JSON format. I tried reading it using


but got an error saying

Error in fromJSON(train$POLYLINE) :
STRING_ELT() can only be applied to a ‘character vector’, not a ‘integer’

How to do this correctly?



You may try this->

d=data.frame(jsoncol=k,stringsAsFactors = FALSE)


<!doctype html>

#The fromJSON function takes a string and train$POLYLINE returns a character vector

d = fromJSON(train$POLYLINE[1])
#The above will work and will return a list object which would have to be transposed so that there are two columns

#For the First row one could get a Dataframe object with numeric values ordered in column [latitude] and column [longitude]$POLYLINE[1])

#Or one could get a List of Dataframe if applied to the train$POLYLINE column or character vector

taxi.route <- lapply(train$POLYLINE, <- function(x){
  #Given a string representing JSON returns a data frame of double
  #Better implementation than spliting and then converting to dataframe
  #convert it into list

  lonlat <- fromJSON(x)
  #change from list to dataframe and correct column, row
  route <- data.frame(lonlat)
  route <- t(route)
  #Sets the names of the dimensions of the matrix
  colnames(route) <- c("latitude", "longitude")
  rownames(route) <- 1:nrow(route)