0

I have a function which takes a vector of .txt files names of a directory and converts them to a data frame:

makeList <- function(x) {
  print(x)
  dataList <- jsonlite::fromJSON(x)
  dataTemp <- as.data.frame(dataList, stringsAsFactors = FALSE)
  data <- select(dataTemp, -hit_lats, -hit_lngs)
  return(data)
}

I applied this function to name of files: DF <- lapply(filenames, FUN = makeList) and finally, bind the resulted list to a data frame:

dFinal <- data.frame()

for(i in 1:length(DF)){
  dFinal <- rbind(dFinal, DF[[i]])
}

when I do not use stringsAsFactors = FALSE in as.data.frame I give some warnnings like: Warning messages:

1: In `[<-.factor`(`*tmp*`, ri, value = c(5.53719008264463,  ... :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = c(1.....
...

but my main problem is that in my data frame two of variables become totally NA. However, I checked this process for some single files (not by the function) and I had not any NA (obviously, I do not run the last part of the code for a single file).

Does anybody know why these NAs have generated?

mjoudy
  • 149
  • 1
  • 1
  • 10
  • 1
    Please provide part of your data (list). – MKR Jun 16 '18 at 11:37
  • 1
    Do you want to bind by rows or column? I presume you're using `dplyr`, so you can probably do `bind_cols(DF)`, or `bind_rows` if that's really what you want. – Alexis Jun 16 '18 at 13:53
  • Thanks a lot. I used `bind_rows` but I got this error: `Error in bind_rows_(x, .id) : Column class_mean can't be converted from character to numeric` and fix it with the help of [this](https://stackoverflow.com/questions/46789010/error-in-bind-rows-x-id-column-cant-be-converted-from-factor-to-numeric). – mjoudy Jun 16 '18 at 14:27

0 Answers0