Reputation: 549
I want to generate a series of 500 TimeStamp starting Jan 1st, 2016 in such a way that the increment of time stamp should look like something as below.
Sample:
TimeStamp
2016-01-01 00:00:01
2016-01-01 00:00:12
2016-01-01 00:00:15
2016-01-01 00:01:23
2016-01-01 00:02:31
2016-01-01 00:02:38
2016-01-01 00:03:48
2016-01-01 00:03:55
.....
What I am doing as of now is:
SampleData <- as.data.frame(list(Var1=1:500, Var2=rnorm(1, 500, 500)))
rDate <- function(sDate, eDate, SampleData){
lenDate <- dim(sampledata)[1]
seqDays <- seq.Date(as.Date(sDate), as.Date(eDate), by="day")
aDay <- runif(lenDate, 1, length(seqDays))
Date <- seqDays[aDay]
}
SampleData$TimeStamp <- rDate("2016-01-01", "2016-12-31", SampleData)
SampleData <- SampleData[order(SampleData$TimeStamp), ]
row.names(SampleData) <- NULL
head(SampleData)
But this will produce the following result:
Var1 Var2 TimeStamp
1 200 1020.469 2016-01-01
2 100 1020.469 2016-01-02
3 344 1020.469 2016-01-02
4 447 1020.469 2016-01-04
5 453 1020.469 2016-01-05
6 478 1020.469 2016-01-05
Which is not what I wanted.
Could someone please help?
Upvotes: 1
Views: 826
Reputation: 389047
Maybe something like,
as.POSIXct("2016-01-01 00:00:00") + sort(sample(1:1000, 500))
We can check this for 5 samples
as.POSIXct("2016-01-01 00:00:00") + sort(sample(1:1000, 5))
#[1] "2016-01-01 00:01:53 IST" "2016-01-01 00:02:06 IST" "2016-01-01 00:03:19 IST"
#[4] "2016-01-01 00:07:31 IST" "2016-01-01 00:12:26 IST"
This will add randomly 1 to 1000 seconds in an incremental fashion in 1st of Jan 2016. To further increase the range we can increase the sequence from 1:1000
to any number you wish.
Another solution which takes advantage of entire range is
startTime <- as.POSIXct("2016-01-01")
endTime <- as.POSIXct("2016-12-31")
sample(seq(startTime, endTime, 1), 500)
Here we generate sequence for every second from our startTime
to our endTime
and then take random 500 values from it. Although, this is complete but this would become slow as the difference between startTime
and endTime
increases.
Upvotes: 1
Reputation: 5647
Here I got something for you...
RandomTimeStamp <- function(M, sDate="2016/01/01", eDate="2016/12/31") {
sDate <- as.POSIXct(as.Date(sDate))
eDate <- as.POSIXct(as.Date(eDate))
dTime <- as.numeric(difftime(eDate, sDate, unit="sec"))
sTimeStamp <- sort(runif(M, 0, dTime))
TimeStamp <- sDate + sTimeStamp
}
print(RandomTimeStamp(500))
This produces the result as:
[1] "2012-01-01 18:26:53 IST" "2012-01-02 11:35:47 IST" "2012-01-02 15:02:23 IST" "2012-01-02 19:19:25 IST"
[5] "2012-01-03 04:48:13 IST" "2012-01-03 21:05:42 IST" "2012-01-03 21:16:06 IST" "2012-01-04 21:05:08 IST"
[9] "2012-01-05 05:47:13 IST" "2012-01-05 06:27:44 IST" "2012-01-05 06:40:42 IST" "2012-01-05 21:56:45 IST"
[13] "2012-01-06 22:36:40 IST" "2012-01-07 03:48:37 IST" "2012-01-07 12:55:25 IST" "2012-01-07 20:52:19 IST" .........
You might want to tweak around the code... :)
Upvotes: 1
Reputation: 13680
Just change seq.Date
into seq.POSIXt
and as.Date
into as.POSIXct
SampleData <- as.data.frame(list(Var1=1:500, Var2=rnorm(1, 500, 500)))
rDate <- function(sDate, eDate, SampleData){
lenDate <- dim(SampleData)[1]
seqDays <- seq.POSIXt(as.POSIXct(sDate), as.POSIXct(eDate), by="secs")
aDay <- runif(lenDate, 1, length(seqDays))
Date <- seqDays[aDay]
}
SampleData$TimeStamp <- rDate("2016-01-01", "2016-12-31", SampleData)
SampleData <- SampleData[order(SampleData$TimeStamp), ]
row.names(SampleData) <- NULL
head(SampleData)
Var1 Var2 TimeStamp
1 29 660.4593 2016-01-01 13:25:31
2 213 660.4593 2016-01-02 07:17:10
3 115 660.4593 2016-01-05 01:07:48
4 358 660.4593 2016-01-05 06:24:41
5 276 660.4593 2016-01-06 10:02:18
6 49 660.4593 2016-01-06 21:56:25
Upvotes: 1