I am trying to remove one hour off of the time in the last column of my data. I have tried using the lubridate package but it is just not working for me. Any solution would be helpful! TIA
Asked
Active
Viewed 70 times
-1

EthanMcQ
- 31
- 4
-
1Just a good practice comment: your data may have started out as a CSV file or an Excel file or a table in a database or from any number of different sources. Once you've read it into R, it's a `data.frame` (in this case, more specifically a `tibble`). Don't call it a CSV anymore--it's not relevant where it came from. – Gregor Thomas Feb 22 '22 at 20:29
-
Great, I appreciate the feedback. I'm new to R and to this site! – EthanMcQ Feb 22 '22 at 20:37
-
Please show the code you tried and tell exactly how it didn't work. Also in the future share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Pictures of data are not helpful because we can't copy/paste that into R for testing. – MrFlick Feb 22 '22 at 20:38
1 Answers
0
The issue can perhaps be fixed at time of import, setting the time zone (see readr's documentation).
Once imported, you can subtract the correspondent time in seconds, like time -3600 (as there are 3600 seconds in a hour), and then you can convert it back using lubridate
with something like:
data$Time <- lubridate::seconds_to_period(as.numeric(data$Time-3600))
To get back to the exact format you had before, after the previous step, you can reformat with something like:
library("lubridate")
data$Time <- lubridate::seconds_to_period(as.numeric(data$Time-3600))
data$Time <- sprintf('%02d:%02d:%02d', hour(data$Time), minute(data$Time), second(data$Time))

giocomai
- 3,043
- 21
- 24
-
Once I do this, it just displays the time in seconds and I need it in the H:M:S format. Is there a way to work around that? – EthanMcQ Feb 22 '22 at 20:41
-
now I see, the Time column comes from readr::parse_time, https://readr.tidyverse.org/reference/parse_datetime.html - you can adjust the timezone when importing, or probably convert back, I'll adjust response – giocomai Feb 22 '22 at 20:55