I'm trying to automatize the attribution of a group number by periods of time. Because I'm writing of function to aggregate time series of weather data by different time periods defined by the user. Let's call "n" the number of sub-periods
d1 = seq(as.Date("1910/1/1"), as.Date("1910/1/20"), "days")
d2 = seq(as.Date("1911/2/4"), as.Date("1911/2/27"), "days")
id1 = rep("1", length(d1))
id2 = rep("2", length(d2))
df = data.frame(date = c(d1,d2), id = c(id1,id2))
df
I would like to cut my dates into a number "n" of periods and to add the periods number to each row of my data frame: Something like that if I want periods of 4 days:
df$period = c(rep(c(1:4), each = length(d1)/4), rep(c(1:4), each = length(d2)/4))
df
I have different length of date for each ID in my real data set. So it's why I want to build the first groups with the same size and the last one with the rest.
Let's imagine I want fourth periods : I wrote this but this is returning me only "4":
df2 =df %>%
group_by(date,id) %>%
mutate(period = c(rep(seq(1,4-1, by = 1), each = as.integer(length(date)/4)),
rep(4, length(date)-((4-1)*as.integer(length(date)/4)))))
df2
Anyone has an idea ?
@hammoire :
So here for example for the first ID I have 20 dates and if I want to cut it into 3 periods : c(1,1,1,1,1,1 ,2,2,2,2,2,2, 3,3,3,3,3,3,3,3)