Change timestamp to date - r

My data format is "Posted Today 10:12 AM". I want to change it to 2017-05-12 10:12:00
How can I change it using R? Thank you in advance for your help.

Try this, your question is incomplete but I'm guessing the format
#assuming this is how your date looks like
a<-data.frame("5/14/17", "10:12 AM")
#giving name
colnames(a)<-c("date", "time")
#convert to your expected format
as.POSIXlt(paste(a$date, a$time), format = "%m/%d/%y %H:%M")

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"

Convert date time in R to date time for time series

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)

Converting yyyy numeric data to date format in R

I have a dataset, df with a column containing dates in yyyy format (ex: 2018). I’m trying to make a time series graph, and therefore need to convert them to a date format.
I initially tried, df$year <- as.Date(df$year) but was told I needed to specify an origin.
I then tried to convert to a character, then a date format:
df$year <- as.character(df$year)
df$year <- as.Date(df$year, format = “%Y”)
This seems to have worked, however when it changed the all the years to yyyy-mm-dd format, and set the month and day to April 5th, today. For example 2018 becomes 2018-04-05.
Does anyone have an idea for how to fix this? I would like it to start on January 1, not the day I am performing the conversion. I also tried strptime(as.character(beer_states$year), “%Y”) with the same result.
Any help would be very much appreciated. Thanks!
Add an arbitrary date and month before converting to date.
df$Date <- as.Date(paste(df$year, 1, 1), '%Y %m %d')
We can use as.yearmon
library(zoo)
df$Date <- as.Date(as.yearmon(df$year, '-01'), '%Y-%m'))

R Converting from datetime to date

I have to do a difference between dates and return the number of days.
the format of the dates is as follows:
12/9/2011 12:00:00 AM
Does anyone know how to perform the difference without using lubridate?
We can use asPOSIXct to convert to DateTime
v1 <- as.POSIXct("12/9/2011 12:00:00 AM", format = "%d/%m/%Y %I:%M:%S %p")
If we need only Date
as.Date(v1)
Convert datetime column in date format
df$DATETIME<-as.Date(df$DATETIME)

convert date and time string to POSIX in R

I have a date and time as this:
"2013-09-05 0:00am"
I need to convert this to POSIX and I tried this:
as.POSIXct(c("2013-09-01 0:00am"), format="%Y-%m-%d %I:%M%p")
I am getting an NA output, any ideas why?
Because there's no such thing as "00:00 AM". If you're using an AM/PM indicator, then the only possible values for hours are 1-12. If you want to use hours 00-24, then you can't use an AM/PM indicator.
I think there is no 0:00am.
Try
as.POSIXct(c("2013-09-01 1:00am"), format="%Y-%m-%d %I:%M%p",tz="America/New_York")
and it works.

Resources