Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

generating a column of times

I’m attempting to create a column of just times in my dataset. I’ve generated a column of random dates from 2018-2020 but the time stamps don’t seem to be generating randomly throughout the day as I’d like.

I did this to create the date/time column

data$date <- sample(seq(as.POSIXct('2018/01/01'), as.POSIXct('2020/12/31'), by = "day"),
                    length(data$date), replace = TRUE)

and am using this to pull the times

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data$time <- format(data$date, format = "%H:%M:%S")

but this is what it looks like

> dput(data[1:10,-c(5,6)])
structure(list(order_num = c(501073L, 969942L, 1091101L, 590143L, 
390404L, 219429L, 1025827L, 689629L, 694348L, 435848L), date = structure(c(1542344400, 
1552194000, 1550379600, 1534568400, 1523336400, 1563426000, 1595826000, 
1552712400, 1534309200, 1547960400), class = c("POSIXct", "POSIXt"
), tzone = ""), total_sale = c(36.3853391310075, 35.9405038506853, 
55.6254974332793, 47.7214780063544, 61.4086594373677, 32.8631076291332, 
33.3640439679803, 40.8944394660076, 54.9455495252506, 48.12597580998
), season = c("Spring", "Winter", "Winter", "Fall", "Fall", "Spring", 
"Summer", "Summer", "Fall", "Fall"), time = c("00:00:00", "00:00:00", 
"00:00:00", "01:00:00", "01:00:00", "01:00:00", "01:00:00", "01:00:00", 
"01:00:00", "00:00:00")), row.names = c(NA, 10L), class = "data.frame")

I’m hoping to get more random times throughout the day like 9:33:35, 14:56:43, etc.

>Solution :

You can generate random times using –

data$time <- format(as.POSIXct(sample(86400, nrow(data)), origin = '1970-01-01'), '%T')

This generates random numbers from 1 to 86400 (seconds in a day) changes it to POSIXct type and extracts only the time from it using format.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading