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
  • 292
  • 1
  • 9

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
  • 355,584
  • 18
  • 123
  • 178
1

We can use rbindlist from data.table

library(data.table)
dfall <- rbindlist(list(dfall, dfiteration), fill = TRUE)
akrun
  • 789,025
  • 32
  • 460
  • 575