I am working with the R programming language.
I made the following dataset:
> id = 1:100
> d1 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100)
> d2 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100)
> d3 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100)
> d4 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100)
> d5 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100)
>
> my_data = data.frame(id, d1, d2, d3, d4, d5)
> head(my_data)
id d1 d2 d3 d4 d5
1 1 2005-08-15 2009-08-31 2013-01-01 2010-01-19 1999-03-14
2 2 2008-06-28 2010-04-10 2007-01-29 2000-09-10 2019-09-11
3 3 2009-03-14 2001-04-05 2015-10-14 2008-09-12 2012-07-15
4 4 2003-06-12 2007-01-18 2016-07-19 2009-12-19 2014-02-08
5 5 2006-11-14 2006-11-05 2004-07-22 2011-05-25 2003-07-03
6 6 2001-11-28 2006-03-25 2017-06-20 2019-04-25 2012-03-16
- For each row, I would like : d1 < d2 < d3 < d4 <d5
I tried to do this using the following code in which each previous "d_i" variable acts as the minimum value that the successive "d_i 1" variable can take :
id = 1:100
d1 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100)
my_data = data.frame(id, d1)
my_data$d2 = sample(seq(my_data$d1, as.Date('2020/01/01'), by="day"), 100)
my_data$d3 = sample(seq(my_data$d2, as.Date('2020/01/01'), by="day"), 100)
my_data$d4 = sample(seq(my_data$d3, as.Date('2020/01/01'), by="day"), 100)
my_data$d5 = sample(seq(my_data$d4, as.Date('2020/01/01'), by="day"), 100)
But this returns the following error:
Error in seq.Date(my_data$d1, as.Date("2020/01/01"), by = "day") :
'from' must be of length 1
Can someone please show me how to do this?
Thanks!
CodePudding user response:
my_data = data.frame(id = 1:100,
d1 = sample(seq(as.Date('1999/01/01'), as.Date('2020/01/01'), by="day"), 100),
d2 = as.Date(NA),
d3 = as.Date(NA),
d4 = as.Date(NA),
d5 = as.Date(NA)
)
for(i in 1:nrow(my_data)){
for(j in 2:5){
prev <- my_data[i, j]
my_data[i, j 1] <- sample(seq(as.Date(prev), as.Date('2020/01/01'), by="day"), 1)
}
}