R datetime scaling [duplicate] - r

This question already has an answer here:
Extract time from datatime in R
(1 answer)
Closed 6 years ago.
My dataset df has datetime column,
this column's format is YYYY-MM-DD HH:MM
I made new column that has only YYYY-MM-DD using
df$date <- factor(as.Date(df$datetime))
but I can't make column that has only time,
I want to make new column 'time' that has format HH:MM

Use strptime and strftime, the former convert character vectors to POSIXct and POSIXlt class( or date time class) and the latter does the opposite. With the format parameter, you can easily convert and format your datetime:
s = "2016-01-01 10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%H:%M")
# [1] "10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%Y-%M-%d")
# [1] "2016-20-01"

Related

How to convert datetime from character to normal datetime in R [duplicate]

This question already has an answer here:
Milliseconds in POSIXct Class
(1 answer)
Closed 1 year ago.
I have a dataframe in r which consists of a column with the following DateTime format with character class.
dt<-2021-03-01 06:10:31.346
I need to convert it into the following format with datetime class.
2021-03-01 06:10:31
I have tried the following solution but it didn't work.
Df$Dt<- as.POSIXct(Df$Dt,format="%Y-%m-%dT%H:%M:%S")
The T is not needed as it was not present in the original string
as.POSIXct(dt, format="%Y-%m-%d %H:%M:%S")
#[1] "2021-03-01 06:10:31 CST"
If we want to take care of the microseconds
as.POSIXct(dt, format="%Y-%m-%d %I:%M:%OS")
Also, the format is not really needed here as the input is in the accepted format for as.POSIXct
as.POSIXct(dt)
#[1] "2021-03-01 06:10:31 CST"
data
dt <- '2021-03-01 06:10:31.346'

how to covert dates with just year-month into year-month-date in R? [duplicate]

This question already has an answer here:
Generating a date from a string with a 'Month-Year' format
(1 answer)
Closed 4 years ago.
I have a list of program dates as character strings in the following format
program.date.have <-c('Sep-14','Aug-14','Sep-16')
I am assuming that all these programs started on the first day of each month, and I want the program.date to end up like
program.date.want<-c('2014-09-01', '2014-08-01, '2016-09-01') or in YYYY-MM-DD format.
To start somewhere I have decided to covert the character strings into the date format in the following way
program.date.have<-c('Sep-14','Aug-14','Sep-16')
betterDates <- as.Date(program.date,
format = "%m-%y")
But even that does not seem to work. how do I use values in program.date variable to be converted into format I want in program.date.want
We can use as.yearmon from zoo, specify the format, and wrap with as.Date which automatically generates the 'day' as the first of the month.
library(zoo)
as.Date(as.yearmon(program.date.have, "%b-%y"))
#[1] "2014-09-01" "2014-08-01" "2016-09-01"
Or a base R option is to paste the '01' at the start or end and then specify the appropriate format in as.Date
as.Date(paste0(program.date.have, "-01"), "%b-%y-%d")
#[1] "2014-09-01" "2014-08-01" "2016-09-01"

Converting from POSIXct to character in R [duplicate]

This question already has an answer here:
Convert date to character in a data frame
(1 answer)
Closed 5 years ago.
I have a POSIXct datetime column in a file.
$date
[1] "POSIXct" "POSIXt"
"2005-10-13 14:13:08 UTC"
I would like to convert it to character column.
Any ideas?
Just cast your POSIXCT object to character, e.g.
date <- as.POSIXct("20-Jan-2018 19:06:08.314", format="%d-%b-%Y %H:%M:%OS")
date.str <- as.character(date)
date.str
[1] "2018-01-20 19:06:08"

How to convert this timestamp format to standard timestamp format? [duplicate]

This question already has an answer here:
Dealing with timestamps in R
(1 answer)
Closed 6 years ago.
The timestamps I have as character class are in this format: 1/28/15 16:34 . How do I covert it to an R time stamp format and then also extract the hour of the day, day of the week and year separately as well?
You can use strptime function in this way:
my_time = strptime("1/28/15 16:34", "%m/%d/%y %H:%M")
Note in particular the %m and the %y to say, respectively, that months will be written with 1 character from Jan to Sept and year will be written with 2 character.
For example, if you need to convert "01/28/2015" you need %M and %Y:
my_time = strptime('01/28/2015 16:34', '%M/%d/%Y %H:%M')
To extract the day of week and the hour:
library(lubridate)
week_day = wday(my_time) # or wday(my_time, label=T) if you want the weekday label (Wed in this case)
day_hour = hour(my_time)

how to convert date string from csv file into date format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert String of anyformat into dd-mm-yy hh:mm:ss in R
Having date value in excel file is like 12/12/2010 I need to extract the date from the Excel file in dd-mm-yyy hh:mm:ss format. How can I do that in R?
When I tried using
strftime(as.POSIXct(as.Date(startdate)), format="%d-%m-%Y %H:%M:%S", tz="UCT")
to do this with the date 12/12/2000,
I got the result like this: `0012-12-20 00:00:00`.
Thank you for your help.But i need to convert any date format into
"dd-mm-yyyy hh mm ss".
that if i give any format of dates like - dd-mm-yyyy or mm-dd-yyyy or yyyy-mm-dd any of these i need the resulted format in "dd-mm-yy hh mm ss"
How about this:
as.Date("12/31/2000", format="%m/%d/%Y")
which yields a Date object, or:
d1 <- strptime("12/31/2000 17:35:17", format="%m/%d/%Y %H:%M:%S")
class(d1)
[1] "POSIXlt" "POSIXt"
d1
[1] "2000-12-31 17:35:17"

Resources