I am working on Rstudio and I'm trying to retrieve time from a date-time column. for example, the date-time column looks like this in my vector '2020-01-01 00:33:03'. This is only one observation I have in the column I want to extract information from.
Anyway, When I apply strftime('2020-01-01 00:33:03', '%H') and want to extract hours in this case. The function does not give me the correct hour which is in this cases 12 AM instead it gives me 16. I am not sure if there is anything I should specify further. This is New York Time idk if that helps.
I really appreciate any feedback or if there is any other functions/packages that I should use other than this one.
new york should be eastern time zone. Have you tried to first give it a timezone before acquiring the hour?
t <- strptime( '2020-01-01 00:33:03',
format = "%Y-%m-%d %H:%M:%OS",
tz = "EST")
strftime(t, '%H')
You can acquire your actual tz also with Sys.timezone(location = TRUE)
Related
I have loaded in time series data of creek depth and am needing to calculate total annual values (Oct-April) for each year. The following is what I have tried thus far:
depth <- read_csv("Depth.Water_Depth-_All_Instruments#GCNP_-_Robber's_Roost_Spring.EntireRecord .csv")
The following is a screenshot of the resulting data frame
enter image description here
These are my attempts at making the timestamp column (ISO_UTC) into a date class. Although, each attempt makes all values for ISO_UTC into N/A values instead of dates / times.
ymd_hm(depth$ISO_UTC)
depth$ISO_UTC<- as.Date(depth$ISO_UTC, format = "%Y-%m-%dT%H:%M")
depth$ISO_UTC<- as.POSIXct(depth$ISO_UTC, format = "%Y-%m-%d %H:%M", tz = "US/Pacific")
Please help me to put these data into usable datetime values.
Please see the above details.
I have obtained data from a remote tagging experiment and the dates are in SAS format (43255.940972). The last level of information I want to extract are seconds. I was trying this:
as.Date(data$Date, origin = "1960-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
But all I get in return is Year-Month-Day but no time details. How do I specify, using this command that I also want that information to be returned?
Thanks
Based on the answer provided by #Onyambu
as.POSIXct(a*86400,origin="1899-12-30")
Some documentation you can read on those functions and date time in general.
I am attempting to convert a column in my data set from fctr to date format. The current column has data formatted as follows: "01/01/14. 01:00 Am." Ideally I would like to create a column for day and then a column for time as well. There are periods following the day and the time which is another issue I am facing. So far I have attempted to use lubridate to create a new column of data but I get the error "All formats failed to parse. No formats found." Any help would be greatly appreciated, thank you.
test <- fourteen %>%
mutate(When = mdy_hms(V3))
View(test)
If your date factor literally has levels that look like 01/01/14. 01:00 Am. including two periods and a space between the first period and the first hour digits and a space between the minutes and the am/pm designation, and all the dates are in this format, then the following should work:
... mutate(When = as.POSIXct(V3, format="%m/%d/%y. %H:%M %p.")) ...
In particular, the following standalone testcase works fine:
as.POSIXct(factor("01/01/14. 01:00 Am."), format="%m/%d/%y. %H:%M %p.")
For more information on the format argument being used here, see the R help page for the function strftime.
I'm importing data from Excel and then trying to manipulate dates and times in R and it's giving me SUCH A HEADACHE. In the Excel file, one column contained a date and time, and another column contained a different time for that same day. The data in R looks basically like this example:
mydata <- data.frame(DateTime1 = as.POSIXct(c("2014-12-13 04:56:00",
"2014-12-13 09:30:00",
"2014-12-13 11:30:00",
"2014-12-13 13:30:00"),
origin = "1970-01-01", tz = "GMT"),
Time2 = c(0.209, 0.209, 0.715, 0.715))
I'd like to have a new column in POSIXct format with the date and the 2nd time, and I can't get that to work. I've tried:
mydata$DateTime2 <- as.POSIXct(as.numeric(as.Date(mydata$DateTime1)
+ mydata$Time2), origin = "1970-01-01",
tz = "GMT")
but that gives me dates and times close to 1/1/1970.
This is more convoluted, but one thing that has worked in other similiar situations that I've also tried is:
library(lubridate)
mydata$DateTime2 <- ymd_hms(format(as.POSIXct(as.Date(mydata$DateTime1) +
mydata$Time2,
origin = "1899-12-30", tz = "GMT")))
but that gives me dates and times that are off by 8 hours. That time difference makes me think that the problem is the time zone since I'm on Pacific Standard Time, but I set it to GMT both in the input data and when trying to convert! What gives? I'm hesitant to just add 8 hours to everything because of daylight savings time complications.
Really, both of the attempts I'm listing here seem to have problems with interconversion, i.e., if you start with a POSIXct object and convert it to numeric and then convert it back again to POSIXct, you should end up back where you started and you don't. Similarly, if you start with the time zone being GMT and then you add something that also is set to have the time zone as GMT, then you shouldn't have a problem with things somewhere mysteriously getting converted to the system time zone.
Advice?
I found an answer based on chris holbrook's answer here: How do you convert dates/times from one time zone to another in R?
This worked:
mydata$DateTime2 <- as.POSIXct(as.Date(mydata$DateTime1) +
mydata$Time2)
attributes(mydata$DateTime2)$tzone <- "GMT"
#MichaelChirico and I were correct that the time zone was the problem. I'm still not sure why, but the time zone for DateTime2 was apparently PST. It didn't list "PST" when I checked str(mydata$DateTime2), but based on the time difference, it must have been, in fact, PST until I set the attributes. Crazy. It did that even though DateTime1 was GMT.
I have one question. How to convert that format 20110711201023 of date and time, to the number of hours. This is output of software which I use to image analysis, and I can’t change it. It is very important to define starting Date and Time.
Format: 2011 year, 07 month, 11 day, 20 hour, 10 minute, 23 second.
Example:
Starting Data and Time - 20110709201023,
First Data and Time - 20110711214020
Result = 49,5h.
I have 10000 data in this format so I don't want to do this manually.
I will be very gratefully for any advice.
Best is to first make it a real R time object using strptime:
time_obj = strptime("20110711201023", format = "%Y%m%d%H%M%S")
If you do this with both the start and the end date, you can simply say:
end_time - start_time
to get the difference in seconds, which can easily be converted to number of hours. To convert a whole list of these time strings, simply do:
time_vector = strptime(dat$time_string, format = "%Y%m%d%H%M%S")
where dat is the data.frame with the data, and time_string the column containing the time strings. Note that strptime works also on a vector (it is vectorized). You can also make the new time vector part of dat:
dat$time = strptime(dat$time_string, format = "%Y%m%d%H%M%S")
or more elegantly (at least if you hate $ as much as me :)):
dat = within(dat, { time = strptime(dat$time_string, format = "%Y%m%d%H%M%S") })