You can also try:
library(dplyr)
library(tidyr)
#Code
new <- df %>%
#Create group
mutate(Dates=as.Date(Dates),
Group=ifelse(Dates>=as.Date('2020-01-01') & Dates<=as.Date('2020-05-30'),'Seg1',
ifelse(Dates>=as.Date('2020-06-01') & Dates<=as.Date('2020-08-31'),'Seg2',
ifelse(Dates>=as.Date('2020-09-01') & Dates<=as.Date('2020-12-31'),'Seg3',NA)))) %>%
group_by(Geography,Group) %>%
select(-Dates) %>%
summarise_all(mean,na.rm=T) %>%
pivot_wider(names_from = Group,values_from=c(var1:var3))
Output:
# A tibble: 3 x 7
# Groups: Geography [3]
Geography var1_Seg1 var1_Seg3 var2_Seg1 var2_Seg3 var3_Seg1 var3_Seg3
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A 20 50 200 500 2 5
2 B 60 60 600 600 6 6
3 C 40 70 400 700 4 7
Althought, @akrun solution is more efficient in terms of creating the groups and avoid conditionals.
Some data used:
#Data
df <- structure(list(Geography = c("A", "A", "A", "A", "B", "B", "B",
"B", "C", "C", "C", "C"), Dates = c(" 2020-01-01 ", " 2020-01-02 ",
" 2020-01-03 ", " 2020-12-31 ", " 2020-01-01 ", " 2020-01-02 ",
" 2020-01-03 ", " 2020-12-31 ", " 2020-01-01 ", " 2020-01-02 ",
" 2020-01-03 ", " 2020-12-31 "), var1 = c(10L, 20L, 30L, 50L,
60L, 70L, 50L, 60L, 30L, 40L, 50L, 70L), var2 = c(100L, 200L,
300L, 500L, 600L, 700L, 500L, 600L, 300L, 400L, 500L, 700L),
var3 = c(1L, 2L, 3L, 5L, 6L, 7L, 5L, 6L, 3L, 4L, 5L, 7L)), row.names = c(2L,
4L, 6L, 9L, 11L, 13L, 15L, 18L, 20L, 22L, 24L, 27L), class = "data.frame")