extracting hour and minute from character column in r [duplicate] - r

This question already has answers here:
How to convert time stamp string "2014-07-20T05:11:49.988Z" into POSIXt in R?
(2 answers)
Closed 6 years ago.
I have the following data frame,the data set is already imported from a database table and created_at column has character type:
sale_id created_at
1 2016-05-28T05:53:31.042Z
2 2016-05-30T12:50:58.184Z
3 2016-05-23T10:22:18.858Z
4 2016-05-27T09:20:15.158Z
5 2016-05-21T08:30:17.337Z
6 2016-05-28T07:41:14.361Z
How can i extract only hour and minute from created_at column , preferably using base r libraries? i need to paste hour and minute together later and put it as a new column.

We can use the convenient functions in lubridate to convert the character column to DateTime and extract the hour and minute with format
library(lubridate)
v1 <- ymd_hms("2016-05-28T05:53:31.042Z")
format(v1, "%H:%M")
#[1] "05:53"
Or using only base R
format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"), "%H:%M")
#[1] "05:53"
Other options include with gsub
gsub(".*T|:\\d+\\..*", "", "2016-05-28T05:53:31.042z")
#[1] "05:53"

Using only base R libraries:
format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"), "%H:%M")
05:31
It appears that's UTC format. For more details on parsing that format see this.
Let me show it using Sys.Date() for an example as well:
format(as.POSIXlt(Sys.time(), "America/New_York"), "%H:%M")
08:15
Using the infinitely better lubridate library:
require(lubridate)
minute(ymd_hms("2016-05-28T05:53:31.042Z"))
53
second(ymd_hms("2016-05-28T05:53:31.042Z"))
31.042

Related

R: converting original excel dates to the format yyyy-mm-dd HH:MM:SS [duplicate]

This question already has answers here:
Converting excel DateTime serial number to R DateTime
(5 answers)
Closed 7 months ago.
I've read an excel files with dates in different formats into R. Some are correctly read in the format "yyyy-mm-dd HH:MM:SS" and those who have had another format in excel before are now numbers like:
44586.727083333302 (stands for 25.01.2022 17:27:00)
I tried to convert them:
as.Date(df$dates, format='%Y-%m-%d %H:%M:%S', origin = "1900-01-01 24:00:00")
But R gives me just yyyy-mm-dd and HH:MM:SS is missing.
I need the timestamp as well. Does anyone know how the code must be?
You'll have to use the as.POSIXct() function instead of the as.Date() function. as.Date() returns the day without the time. The formatting you did should be the same.
Update: request OP:
You should install parsedate package:
library(parsedate)
#your df:
date
1 2018-06-30 12:09:34
2 44586.727083333302
# with this code:
dat %>%
mutate(x = parse_date(date))
you get this
date x
1 2018-06-30 12:09:34 2018-06-30 12:09:34
2 44586.727083333302 2022-07-31 15:39:06
First answer:
We could use the convertDateTime function:
library(openxlsx)
string <- 44586.727083333302
convertToDateTime(string, origin = "1900-01-01")
#or for your data frame:
convertToDateTime(df$dates, origin = "1900-01-01")
[1] "2022-01-25 17:27:00 CET"

Converting YY/MM/DD to MM/DD/YY in R [duplicate]

This question already has answers here:
How to convert date to format "yyyy-mm-dd" in R when input value can be of different formats
(3 answers)
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 2 years ago.
I need to find the difference in days between 2 date columns, but one of them is in the format of "6/16/2019" and the other is in the format of "2019-02-25". Not sure which one would be easier to convert to which, but would like to get end result in days. Which I know how to do. I would appreciate help converting the second yyyy-mm-dd to mm-dd-yyyy.
We can use functions from the lubridate package to convert the different formats to dates, and then subtract.
rawData <- "date1,date2
2002-05-15,6/16/2019
2019-12-31,4/15/2020"
data <- read.csv(text = rawData,stringsAsFactors = FALSE)
library(lubridate)
mdy(data$date2) - ymd(data$date1)
...and the output:
> mdy(data$date2) - ymd(data$date1)
Time differences in days
[1] 6241 106
>

Changing time column to POSIXct without a date value [duplicate]

This question already has answers here:
Extracting time from POSIXct
(7 answers)
Closed 2 years ago.
mutate(Time = as.POSIXct(Time, format = "%H:%M:%S"))
I'm currently trying to mutate my time variable to that instead of being a character variable, it is in the as.POSIXct format. But, everytime I apply this format, I always get this output: 2020-02-29 07:25:00. The time normally looks like this in just the normal character form: 07:25:00 .
How do I get rid of the date??
Try using the hms package ("hours minutes seconds").
library(hms)
Time <- "2020-02-29 07:25:00"
as_hms(as.POSIXct(Time))
# 07:25:00

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"

date format in R [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 5 years ago.
I have a date column in a data frame in chr format as follows:
chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"
I want to convert to date using something like:
strptime(x=data$dob, '%d-%b-%y')
But I get several future dates in the result like
[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"
Is there a way to ensure I return dates that commenced in the correct century?
Thanks
It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.
Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).
# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')
# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
library(lubridate)
DD=dmy(dd)
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf
strptime(data$dob, "%Y/%m/%d")

Resources