Repeat date twice in a dataframe [duplicate]
I would like to make a change to the data frame below. Notice that in date2
, I have the sequence of 10 dates, counting from the day 2018-01-01
. So far ok, however I would like to have the same date twice, i.e. I would have 2018-01-01
for Category
ABC
and 2018-01-01
for EFG
and so on. How to adjust?
df1 <- data.frame( Id = rep(1:5, length=10),
date1 = as.Date( "2022-01-05"),
date2= seq( as.Date("2018-01-01"), length.out=10, by=1),
Category = rep(c("ABC", "EFG"), length.out = 10))
Id date1 date2 Category
1 1 2022-01-05 2018-01-01 ABC
2 2 2022-01-05 2018-01-02 EFG
3 3 2022-01-05 2018-01-03 ABC
4 4 2022-01-05 2018-01-04 EFG
5 5 2022-01-05 2018-01-05 ABC
6 1 2022-01-05 2018-01-06 EFG
7 2 2022-01-05 2018-01-07 ABC
8 3 2022-01-05 2018-01-08 EFG
9 4 2022-01-05 2018-01-09 ABC
10 5 2022-01-05 2018-01-10 EFG
Use rep(..., each=2)
for that.
df1 <- data.frame( Id = rep(1:5, length=10),
date1 = as.Date( "2022-01-05"),
date2= rep(seq( as.Date("2018-01-01"), length.out=5, by=1), each = 2),
Category = rep(c("ABC", "EFG"), length.out = 10))
df1
# Id date1 date2 Category
# 1 1 2022-01-05 2018-01-01 ABC
# 2 2 2022-01-05 2018-01-01 EFG
# 3 3 2022-01-05 2018-01-02 ABC
# 4 4 2022-01-05 2018-01-02 EFG
# 5 5 2022-01-05 2018-01-03 ABC
# 6 1 2022-01-05 2018-01-03 EFG
# 7 2 2022-01-05 2018-01-04 ABC
# 8 3 2022-01-05 2018-01-04 EFG
# 9 4 2022-01-05 2018-01-05 ABC
# 10 5 2022-01-05 2018-01-05 EFG