I need to union
a result set into this data.frame, in order to make sure I have the columns in place, even if the result set does not contain them. This is for the purposes of writing to a MySQL DB later on.
dbNames <- c('a','b','c','d')
emptyTableOut <- data.frame(
cbind(
matrix(character(), ncol = 1, nrow = 0), # needs to be char
matrix(integer(), ncol = 3, nrow = 0) # needs to be int
), stringsAsFactors = FALSE) %>%
setNames(nm = c(dbNames))
> glimpse(emptyTableOut)
Observations: 0
Variables: 4
$ a <chr>
$ b <chr>
$ c <chr>
$ d <chr>
How can I do this in a way that doesn't coerce the int
s to char
s?
This question is different than the already posted answers because I have a huge number of columns, not the few implied by this minimally reproducible example.