0

I encountered the following error while performing the loop function in order to import my data into R

Error in vctrs::data_frame(): ! Can't recycle users (size 117) to match places (size 3)

The code I used is as follows:

filesU <- list.files("C:/Users/artde/OneDrive/Documents/Master R/Master R/Project 1/Json/Daran_API_U/")

tweetsU = fromJSON("C:/Users/artde/OneDrive/Documents/Master R/Master R/Project 1/Json/Daran_API_U/users_1344795476151971842.json", flatten=T)

filesU <- filesU[-1] 

for (file in 1:length(filesU)) { 
    tweetsU_loop = fromJSON(paste0("C:/Users/artde/OneDrive/Documents/Master R/Master R/Project 1/Json/Daran_API_U/", 
                                   filesU[file])) 
    tweetsU = bind_rows(tweetsU,tweetsU_loop) 
}

However I keep getting this error after performing the bind part

Error in vctrs::data_frame(): ! Can't recycle users (size 117) to match places (size 3). Run rlang::last_error() to see where the error occurred.

With another data set this has worked perfectly so far, however in this dataset it seems that there is a file that creates a problem and does not allow the loop and bind function to work I understand that is due to one of these files having a different length or value compared to the other files, but considering i have 640 files, i was wondering if there was a way to pinpoint the responsible one (there is no obvious one, which has a very different name or is way heavier than every other)

All the files are in Json

user438383
  • 5,716
  • 8
  • 28
  • 43
Daworn
  • 55
  • 6
  • Can't you tell from the value of `file` when the loop crashes? – Andrew Gustar Jun 30 '22 at 10:16
  • So if i remove the file 117, the error appears again, with the same number, it will just give the value 117 to a different file – Daworn Jun 30 '22 at 11:32
  • No, not the number in the error message, but the value of the loop variable `file` just after it crashes. `filesU[file]` will be the problem file. – Andrew Gustar Jun 30 '22 at 12:31
  • If i perform a backtrace i get this Backtrace: x 1. +-dplyr::bind_rows(tweetsU, tweetsU_loop) 2. | \-dplyr:::map(...) 3. | \-base::lapply(.x, .f, ...) 4. | \-dplyr FUN(X[[i]], ...) 5. | \-vctrs::data_frame(!!!.x, .name_repair = "minimal") 6. \-vctrs::stop_incompatible_size(...) 7. \-vctrs:::stop_incompatible(...) 8. \-vctrs:::stop_vctrs(...) 9. \-rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call)) But i am unsure on how to find the file the value of the loop variable – Daworn Jul 01 '22 at 06:18
  • Sorry for the many edits If i run file by itself it gives me a value of 1 – Daworn Jul 01 '22 at 06:22
  • So it is crashing on the `bind_rows` of the first file, which perhaps means that `tweetsU_loop` is not a dataframe, because the JSON file does not represent a simple rectangular array of data. You will need to have a look at the structure of the first `tweetsU_loop` and then work out the best way of merging them all, as `bind_rows` is probably not the way to go in this case. – Andrew Gustar Jul 01 '22 at 08:30
  • Thank you ! So to resume, there is something in this code line that isn't working, especially regarding the bind row function tweetsU_loop = fromJSON(paste0("C:/Users/artde/OneDrive/Documents/Master R/Master R/Project 1/Json/Daran_API_U/", filesU[file])) Thank you very much for your help and your patience – Daworn Jul 01 '22 at 09:15
  • I think the code is working, it is just that the JSON files are not producing the sort of data structure that you expect. Good luck! – Andrew Gustar Jul 01 '22 at 14:33
  • Thank you ! You were right, the structure is different from expected – Daworn Jul 02 '22 at 15:06

0 Answers0