1

I try to run in a loop some api call

I have a dataframe which saves in every iteration all data

However there are some iterations which don't have a specific column

Is there any easy way to save it with an NA without needing to know in every iteration which variable doesn't exist

This is what I use to save the data:

dfall <- rbind(dfall, dfiteration)
Erik Bodg
  • 302
  • 2
  • 10

2 Answers2

1

Use dplyr::bind_rows which will automatically add NA for columns which are not present.

dfall <- dplyr::bind_rows(dfall, dfiteration)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

We can use rbindlist from data.table

library(data.table)
dfall <- rbindlist(list(dfall, dfiteration), fill = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662