Extracting Time from Posixct

Extracting time from POSIXct

You can use strftime to convert datetimes to any character format:

> t <- strftime(times, format="%H:%M:%S")
> t
[1] "02:06:49" "03:37:07" "00:22:45" "00:24:35" "03:09:57" "03:10:41"
[7] "05:05:57" "07:39:39" "06:47:56" "07:56:36"

But that doesn't help very much, since you want to plot your data. One workaround is to strip the date element from your times, and then to add an identical date to all of your times:

> xx <- as.POSIXct(t, format="%H:%M:%S")
> xx
[1] "2012-03-23 02:06:49 GMT" "2012-03-23 03:37:07 GMT"
[3] "2012-03-23 00:22:45 GMT" "2012-03-23 00:24:35 GMT"
[5] "2012-03-23 03:09:57 GMT" "2012-03-23 03:10:41 GMT"
[7] "2012-03-23 05:05:57 GMT" "2012-03-23 07:39:39 GMT"
[9] "2012-03-23 06:47:56 GMT" "2012-03-23 07:56:36 GMT"

Now you can use these datetime objects in your plot:

plot(xx, rnorm(length(xx)), xlab="Time", ylab="Random value")

Sample Image


For more help, see ?DateTimeClasses

How can I extract date and time from a POSIXct element of a time series using my own function?

Convert the seconds since Jan 1, 1970 into date/time.

growthfun<-function (a) {
r <- c()
s <- c()
for (i in seq_along(a)) {
if (a[i] >= max(a[1:(i-1)])) {
r <- c(r, as.POSIXct(index(a[i])))
s <- c(s, a[i])
}
else {
next
}
}
data.frame(row.names=as.POSIXct(r, origin = "1970-01-01", tz = ""), s)
}

growthfun(ts_1)
s
2020-04-20 22:31:28 50.13211
2020-04-20 22:44:05 50.23050
2020-04-20 22:53:49 50.42096

Data:

ts_1 <- structure(c(50.1321122972067, 50.0355467742705, 49.9122834457642, 
49.9948860954217, 50.0397819115463, 50.2304961977954, 49.8852887132391,
50.420955209067, 50.3734680543285, 50.2443255196795), .Dim = c(10L,
1L), .Dimnames = list(NULL, NULL), index = structure(c(1587396688,
1587396699, 1587396846, 1587397205, 1587397420, 1587397445, 1587397972,
1587398029, 1587398179, 1587398337), tzone = "", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), .CLASS = "double")

[,1]
2020-04-20 22:31:28 50.13211
2020-04-20 22:31:39 50.03555
2020-04-20 22:34:06 49.91228
2020-04-20 22:40:05 49.99489
2020-04-20 22:43:40 50.03978
2020-04-20 22:44:05 50.23050
2020-04-20 22:52:52 49.88529
2020-04-20 22:53:49 50.42096
2020-04-20 22:56:19 50.37347
2020-04-20 22:58:57 50.24433

Extracting date and hour from Posixct object with strftime

That's because you are specifying the time zone in as.POSIXct, but not in strptime.

timesteps[1,1]
[1] "2013-01-01 EST"

strftime(timesteps[1,1], format = "%Y-%m-%d %H")
[1] "2012-12-31 21"
strftime(timesteps[1,1], format = "%Y-%m-%d %H",tz='EST')
[1] "2013-01-01 00"`

dateandhour = function (timeindex){
return(strftime(timeindex, format = "%Y-%m-%d %H",tz='EST'))
}

timesteps ['Date and Hour'] = sapply(timesteps$`Time Index`, dateandhour)

head(timesteps)
Time Index Date and Hour
1 2013-01-01 00:00:00 2013-01-01 00
2 2013-01-01 00:05:00 2013-01-01 00
3 2013-01-01 00:10:00 2013-01-01 00
4 2013-01-01 00:15:00 2013-01-01 00
5 2013-01-01 00:20:00 2013-01-01 00
6 2013-01-01 00:25:00 2013-01-01 00

extracting time from datetime in R

We can do this using format

format(strptime(str1, "%Y-%m-%d %H:%M:%S"), "%H%M")

data

str1 <- "899-12-30 07:00:00"

Extracting 2 digit hour from POSIXct in R

Try to do this:

strftime(test, format="%H")

to extract hours and

strftime(test, format="%m")

for month

How to extract time form POSIXct and plot?

First, you need to define "day", as opposed to the full time. Then you need to figure out what you mean by "group" ... let's just say you want to aggregate and take the daily mean. Third, you need to make the plot.

b$day <- round.Date(b[,"sample_time"], units="days")
b_agg <- aggregate(list(sample_time=b[,"sample_time"]), by=list(day=b[,"day"]), FUN=mean)
plot(b_agg)

Edit:

Just an additional thought, if you didn't want to aggregate, you could skip the second step, and change the third to plot(b[,"day"], b[,"latitude"]. Alternatively, you may even want something like boxplot(latitude~day, data=b).

Extract date and time from datetime field in R

If I understood well, R can read correctly your dates and times as you import your data (because they are in POSIXct format), but you can not extract the date and the time in the right format from your date-time column.

Considering that you have a data.frame in R, like this:

            date_time Sold
1 2020-01-01 03:16:01 2
2 2020-01-02 02:15:12 2
3 2020-01-03 08:26:11 3
4 2020-01-04 09:29:14 2
5 2020-01-05 12:06:06 1
6 2020-01-06 08:08:11 3

Lubridate does not offer a function to extract the time component, so you have to extract piece by piece with the minute(), hour() and second() functions. Then you can just concatenate these components with paste() function. Now, with the dates, you can use the date() function to extract then, after that, you use the format() function to format these dates in the way you want.

library(lubridate)
library(dplyr)
library(magrittr)

tab <- tab %>%
mutate(
date = as.Date(date_time),
hour = hour(date_time),
minute = minute(date_time),
second = second(date_time)
) %>%
mutate(
format_date = format(date, "%m/%d/%Y"),
format_hour = paste(hour, minute, second, sep = ":")
)

Resulting this:

tab %>% select(format_date, format_hour) %>% head()

format_date format_hour
1 01/01/2020 12:4:23
2 01/02/2020 3:19:13
3 01/03/2020 8:6:24
4 01/04/2020 6:28:2
5 01/05/2020 2:16:20
6 01/06/2020 12:8:28

Extracting the time information from a dataframe in R

You have to use apply by column (2):

apply(Time_df,2,strftime, format="%H:%M:%S")

Time1 Time2 Time3 Time4 Time5
[1,] "07:00:00" "19:00:00" "00:00:00" "00:00:00" "00:00:00"
[2,] "00:00:00" "00:00:00" "00:00:00" "00:00:00" "00:00:00"
[3,] "00:00:00" "00:00:00" "00:00:00" "00:00:00" "00:00:00"
[4,] "06:00:00" "22:00:00" "00:00:00" "00:00:00" "00:00:00"
[5,] "08:00:00" "21:00:00" "00:00:00" "00:00:00" "00:00:00"


Related Topics



Leave a reply



Submit