Convert date time in R to date time for time series - r

I have this dataframe where DT is in char, i would like to convert it into date time format in R so that i can plot a time series
DT Name
12-12-21 1:30 James
01-01-22 12:30 Job
03-02-22 1:00 Seth
03-02-22 1:14 Michael
I explored the following code
time <- as.POSIXct(dataframe$DT, format="%m-%d-%Y %H:%M")
but it returned year as 01-01-0021 instead of 01-01-2021, may i know how could i specify the year so that it could be read as 2021?

Instead of %Y, use %y. :
time <- as.POSIXct(dataframe$DT, format="%m-%d-%y %H:%M")

A possible solution
time<-as.POSIXct(dataframe$DT,format="%m/%d/%y %H:%M")
or
# install.packages("hms")
library(hms)
time <- as.hms(dataframe$DT)

Related

I'm getting NA on date column while trying to change date time format to date only in R programming language

I want to merge two data frames in R programming language using the date as the primary key. While trying to change the date, time format on one of the data frames to date only, im getting NA on the date column. Below is the date time format which i want to change to mm dd yy only.
4/12/2016 0:00
This is the code chunk i used.
sleep_day <- sleep_day %>%
rename(date = sleepday) %>%
mutate(date = as.Date(date,format ="%m/%d/%Y %I:%M:%S %p" , tz=Sys.timezone()))
i am expecting the date column to change from date, time to date alone. ie from mm dd yy 00:00 to mm dd yy. The result i got on the date column is NA in R programming
Your format is not correct:
test <- "4/12/2016 0:00"
as.Date(test,format ="%m/%d/%Y %H" , tz=Sys.timezone())
will work. Look at ?strptime.
As an advice, prefer to work with lubridate library, with has easy-to-use functions, which parse a lot of different formats:
library(lubridate)
mdy_hm(test)
"2016-04-12 UTC"

What's the best way to aggregate date time by hourly interval?

I'm having trouble parsing my date time column that are currently in 'chr' type. I want the date time to be grouped by hour interval and sum corresponding values, then merge two date frames.
a <- c("2016-04-12 12:00:00", "2016-04-12 12:01:00")
b <- c(10, 20)
df_1 <- data.frame(a,b)
names(df_1) <- c('Date', 'Steps')
c1 <- c("4/12/2016 12:00:00 AM", "4/12/2016 05:00:00 PM")
d <- c(20,8)
df_2 <- data.frame(c1,d)
names(df_2) <- c('Date', 'Intensity')
df_1 (with minutes interval) to join df_2 (with hourly interval but the whole day is separated by AM PM)
I have tried converting it using as.POSIXct and ymd to datetime type but it's returning NA values. I tried below code from a post I saw before, it worked but it didn't record the PM time of the day. code below
df_1 <- aggregate(df_1["Steps"],
list(Date=cut(as.POSIXct(df_1$Date), "hour")),
sum)
Also, I wanna remove that AM PM on the second date frame.
While the aggregate for df_1 appears to work fine, for df_2 you need to define the time format, using strptime which converts character objects to "POSIX*t".
aggregate(df_2["Intensity"],
list(Date=cut(strptime(df_2$Date, '%m/%d/%Y %I:%M:%S %p'), "hour")),
sum)
# Date Intensity
# 1 2016-04-12 05:00:00 8
# 2 2016-04-12 12:00:00 20
Explanation:
%m/%d/%Y month, day, year, separated by a slash
the space between date and time
%I:%M:%S hour (12h format), minute, second, separated by a colon
another space
%p the AM/PM indicator
Read ?strptime for different options, since this may also depend on your locale.

Adding/subtracting date-time columns in data-frames

I have a data frame with two text columns which are essentially time-stamps columns namely BEFORE and AFTER and the format is 12/29/2016 4:29:00 PM. I want to compare if the gap between the BEFORE and AFTER time for each row is more than 5 minutes or not. Which package in R will allow subtraction between time stamp?
No need for extra packages, using base R you can achieve the date time comparison.
First coerce your character to a valid date time structure. Here I am using POSIXct:
d1= as.POSIXct("12/29/2016 4:29:00 PM",format="%m/%d/%Y %H:%M:%S")
d2= as.POSIXct("12/30/2016 5:29:00 PM",format="%m/%d/%Y %H:%M:%S")
Then to get the difference in minutes you can use difftime:
difftime(d1,d2,units="mins")
Note the all those functions are vectorized So the same code will work with vectors.
manage PM/AM
to take care of PM/AM we should add %p to the format that should be used in conjonction with %I not %H:
d1= as.POSIXct("12/28/2016 11:53:00 AM",
format="%m/%d/%Y %I:%M:%S %p")
d2= as.POSIXct("12/28/2016 12:03:00 PM",
format="%m/%d/%Y %I:%M:%S %p")
difftime(d2,d1,units="mins")
## Time difference of 10 mins
for more help about date/time format you can read ?strptime.

Extract Time and date from POSIXct

I have a vector with DateTime character ("2014-04-17 23:33:00") and want to make a matrix with date and time as my columns.
This is my code:
dat <- as.POSIXct(dates)
date = data.frame(
date=dat,
time=format(dat, "%H:%M")
)
I took a look at extract hours and seconds from POSIXct for plotting purposes in R and it helped, but the problem is that I only get 00:00 as the time in my time column. It does not extract the time from the dates vector.
Any help is appreciated.
Using the following vector as an example:
dates<- c("2012-02-06 15:47:00","2012-02-06 15:02:00")
dat <- as.POSIXct(dates)
date.df = data.frame(
date=dat,
time=format(dat, "%H:%M")
)
You will obtain the correct times ("%H:%M")
> date.df
date time
1 2012-02-06 15:47:00 15:47
2 2012-02-06 15:02:00 15:02

How can I convert this to month?

6/9/2013 1:15
7/9/2013 1:15
I have a series of data in a column of this format.
I am trying to convert it into a month and store it in a different column.
How can I do it?
Intended output
June
July
I tried using the lubridate library but not able to.
How about
x <- c("6/9/2013 1:15","7/9/2013 1:15")
strftime(as.Date(x, format="%m/%d/%Y %H:%M"), "%B")
# [1] "June" "July"
First convert to a proper date via as.Date() then use the formatting options in strftime to get the month name.

Resources