0

Here is a portion of my data:

>head(state1)
># A tibble: 6 x 5
  date       TYP_INT WEATHER pedday intersection
  <date>       <dbl>   <dbl>  <dbl>        <dbl>
1 2019-01-02       1       2  0.204            1
2 2019-01-04       1      10  0.204            1
3 2019-01-06       1       1  0.204            1
4 2019-01-07       1       1  0.204            1
5 2019-01-10       1       1  0.204            1
6 2019-01-22       1       1  0.612            1

I want to have this data start at the first day of the year and end at the last day and fill in zeros for missing dates. When I try to change

start_val="2019-01-01"

I get the message that I need to use class Date, POSIXlt, or POSIXct. I also tried %d to make it show all the days but that also did not work.

 state1<- read_csv(file.choose())
     library(padr)
     edit<-state1%>% 
       padr::pad(start_val = as.Date(start_val="2019-01-01"),end_val = max(.$date)) %>%fill_by_value(value = 0)
Waldi
  • 39,242
  • 6
  • 30
  • 78
Moe
  • 13
  • 4

1 Answers1

0

The dates are stored as characters in the file, but padr expects Dates.
You could use as.Date to get the expected type:

states1 <- read.table(text="
date       TYP_INT WEATHER pedday intersection
  1 2019-01-02       1       2  0.204            1
2 2019-01-04       1      10  0.204            1
3 2019-01-06       1       1  0.204            1
4 2019-01-07       1       1  0.204            1
5 2019-01-10       1       1  0.204            1
6 2019-01-22       1       1  0.612            1",header=T)


library(padr)

library(padr)

states1 %>% mutate(date = as.Date(date)) %>%  
            padr::pad(start_val =as.Date("2019-01-01"),end_val = max(.$date)) %>%
            fill_by_value(value = 0)

         date TYP_INT WEATHER pedday intersection
1  2019-01-01       0       0  0.000            0
2  2019-01-02       1       2  0.204            1
3  2019-01-03       0       0  0.000            0
4  2019-01-04       1      10  0.204            1
5  2019-01-05       0       0  0.000            0
6  2019-01-06       1       1  0.204            1
7  2019-01-07       1       1  0.204            1
8  2019-01-08       0       0  0.000            0
9  2019-01-09       0       0  0.000            0
10 2019-01-10       1       1  0.204            1
11 2019-01-11       0       0  0.000            0
12 2019-01-12       0       0  0.000            0
13 2019-01-13       0       0  0.000            0
14 2019-01-14       0       0  0.000            0
15 2019-01-15       0       0  0.000            0
16 2019-01-16       0       0  0.000            0
17 2019-01-17       0       0  0.000            0
18 2019-01-18       0       0  0.000            0
19 2019-01-19       0       0  0.000            0
20 2019-01-20       0       0  0.000            0
21 2019-01-21       0       0  0.000            0
22 2019-01-22       1       1  0.612            1
Waldi
  • 39,242
  • 6
  • 30
  • 78