Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

dplyr - complete time series by group in r

I have a dataframe

dat <- data.frame(c("G", "G", "G", "G"), c("G1", "G1", "G2", "G2"), c('2017-01-01', '2017-01-03', '2017-04-02', '2017-04-05'))

colnames(dat) <- c('Country', 'Place', 'date')

I would like to have this output: (complete date for each (country-place) group)

dat <- data.frame(c("G", "G", "G", "G", "G", "G", "G"),
                  c("G1","G1", "G1", "G2", "G2", "G2", "G2"), 
                  c('2017-01-01', '2017-01-03','2017-01-03', 
                    '2017-04-02', '2017-04-03', '2017-04-04', '2017-04-05'))

I have tried:

dat = dat %>% group_by(Country, Place) %>% complete(date)

but it does not work. Can anyone help me with this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do:

dat %>%
  mutate(date = as.Date(date)) %>%
  group_by(Country, Place) %>%
  complete(date = seq.Date(min(date), max(date) , by= "day"))


# A tibble: 7 x 3
# Groups:   Country, Place [2]
  Country Place date      
  <fct>   <fct> <date>    
1 G       G1    2017-01-01
2 G       G1    2017-01-02
3 G       G1    2017-01-03
4 G       G2    2017-04-02
5 G       G2    2017-04-03
6 G       G2    2017-04-04
7 G       G2    2017-04-05

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...