0

I am trying to rbind a date and another variable in a for loop, however, I keep getting numbers like "15791" instead of a date in the final dataset. This is what my code looks like:

uid <- 1:49
dates <- seq(as.Date("2013-03-27"), by = "day", length.out = 111)
dataset <- data.frame()
d <- as.Date(d, Origin = "1970-01-01")

for(u in uid){
  for(d in dates){
      dataset <- rbind(dataset, data.frame(uid = u, dates = d))
    }
}
Frank Li
  • 5
  • 3
  • 1
    Do you need `expand.grid(uid, dates)` ? To use dates in `for` loop use https://stackoverflow.com/questions/59572195/how-to-display-real-dates-in-a-loop-in-r/ – Ronak Shah Jun 06 '20 at 09:34

1 Answers1

0

If I understand you correctly,

data <- tibble() %>% 
          expand(
            uid=1:49,
            date=seq(as.Date("2013-03-27"), by = "day", length.out = 111)
          )

Gives you what you want. Certainly, it produces a tibble with 5439 rows, and 111 x 49 is 5439:

# A tibble: 5,439 x 2
     uid date      
   <int> <date>    
 1     1 2013-03-27
 2     1 2013-03-28
 3     1 2013-03-29
 4     1 2013-03-30
 5     1 2013-03-31
 6     1 2013-04-01
 7     1 2013-04-02
 8     1 2013-04-03
 9     1 2013-04-04
10     1 2013-04-05
Limey
  • 10,234
  • 2
  • 12
  • 32