R refusing to concatenate two tibbles with date, "origin must be supplied"

Viewed 19

I am building a giant tibble of epidemic data out of a list of pairs of tibbles my_tibbles, from two databases (DB1.all and an unlisted DB2.all). DB1.all filters the dates of DB2.all, so I have to pick out the rows containing dates in good_dates. We can call them

combined_dat = c(1:((ncol(DB1.all)+ncol(DB2.all))+1))
for (t in 1:4) {
  for (val in 1:length(my_tibbles[[t]][[2]]) {
    this.DB1 = tibbles[[t]][[3]][[val]][[3]][[2]]
    country_list = sort(unique(this.DB1$location))
    specific_dat = 1:68
    for (country in country_list) {
      DB1.country = this.DB1[which(this.DB1$location==country),]
      DB2.country = DB2.all[which(DB2.all$Country==country),]
      good_dates =
        format(as.Date(sort(unique(DB1.country$date)),origin="1970-01-01"),"%Y-%m-%d")
      country_dates =
        which(DB1.country$date%in%as.Date(good_dates,origin="1970-01-01"))
      DB2.country.good_dates = DB2.country[country_dates,]
      country_dat = as_tibble(merge(merge(DB2.country.good_dates[,1:4],
                                          DB1.country[,-c(1:2)]),
                                    DB2.country.good_dates[,-c(1:4)]))
      weekly_pos_var = as.numeric(country_dat$`% per Country and Week`)
      weekly_cases = as.integer(country_dat$new_cases)
      country_dat$positive_rate_var = weekly_pos_var*weekly_cases
      specific_dat = rbind(specific_dat,country_dat)
    }
    specific_dat = as_tibble(specific_dat)
    specific_dat = specific_dat[-1,]
    my_tibbles[[t]][[3]][[val]][[5]] = specific_dat
    combined_dat = rbind(combined_dat,specific_dat)
    combined_dat = as_tibble(combined_dat)
  }
}

For some reason R is refusing to concatenate specific_dat and combined_dat and keeps saying origin must be supplied. There are no date objects in the specific_dat tibble so I figured I should maybe build a matrix of two rows like this

specific_dat = as_tibble(matrix(rep(1:68,2),ncol=68,nrow=2,byrow=TRUE))
colnames(specific_dat) = c(colnames(DB2.all)[1:4],
                           colnames(DB1.all)[-c(1:2)],
                           colnames(DB2.all)[-c(1:4)],
                           "% per Week Var")
specific_dat$date = format(as.Date(specific_dat$date,origin="1970-01-01"),"%Y-%m-%d")

but if I do this I get an error where the dimensions of specific_dat and country_dat are no longer compatible; specific_dat for some reason now has 69 columns instead of 68 whenever I call rbind(specific_dat,country_dat). (I checked whether specific_dat$date creates a new date row but it shouldn't since date is already in colnames(DB1.all).)

Edit: Here is a reproducible example.

   iso_code continent location    date       Type  Value `Submission Count` `% per Country and We~`
   <chr>    <chr>     <chr>       <date>     <chr> <chr>              <dbl>                   <dbl>
 1 AFG      Asia      Afghanistan 2020-05-31 Clade GH                     1                     0.5
 2 AFG      Asia      Afghanistan 2020-06-07 Clade GH                     1                     0.5
 3 AFG      Asia      Afghanistan 2020-06-14 Clade GH                     1                     0.5
 4 AFG      Asia      Afghanistan 2021-01-24 Clade GH                     1                     0.5
 5 AFG      Asia      Afghanistan 2021-04-11 Clade GH                     1                     0.5
 6 AFG      Asia      Afghanistan 2021-04-18 Clade GH                     1                     0.5
 7 AFG      Asia      Afghanistan 2021-04-25 Clade GH                     1                     0.5
 8 AFG      Asia      Afghanistan 2020-05-31 Clade GH                     2                     0.5
 9 AFG      Asia      Afghanistan 2020-06-07 Clade GH                     2                     0.5
10 AFG      Asia      Afghanistan 2020-06-14 Clade GH                     2                     0.5
0 Answers
Related