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

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

Related

How to change from character to date format? [duplicate]

This question already has answers here:
Convert string to date, format: "dd.mm.yyyy"
(4 answers)
What are the "standard unambiguous date" formats for string-to-date conversion in R?
(8 answers)
Closed 3 months ago.
For my dataset, the Date variable has dates in the format of this example: 19-Feb-03
I want to change the above character format dates in the column to a Date format. (As I have to do time series analysis later on)
I tried using the as.Date() method but it didn't work.
x <- '19-Feb-03'
lubridate::ymd(x)
"2019-02-03"
Not sure whether 19 is year or day. You can try lubridate package
x<-"19-Feb-03"
library(lubridate)
ymd(x)
dmy(x)

How can i substract 30 minutes to my timestamp R [duplicate]

This question already has answers here:
Subtracting 10 minutes each from a vector of times in R?
(2 answers)
Closed 2 years ago.
How can I substract 30 minutes to my TIMESTAMP without loosing the original format?
My format is "%Y%j%H%M".
For example, I want 2020-08-27 06:30:00 to become 2020-08-27 06:00:00.
Thank you!
What is the class of your data? If your data is called df check class(df$column_name). If it is of class POSIXct you can do :
df$new_column <- df$column_name - 30*60
Or with lubridate :
df$new_column <- df$column_name - lubridate::minutes(30)
If the class is something different than POSIXct (eg - character) you need to first change it to POSIXct class before using the answer above. That can be done with
df$column_name <- as.POSIXct(df$column_name, tz = 'UTC')

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
>

Lubridate Date parsing is one year off [duplicate]

This question already has answers here:
date functions in R return wrong year
(2 answers)
Closed 3 years ago.
Within R, I'm trying to convert a text string into a Date variable type using lubridate's as.Date function.
I have a vector of values such as:
Dates
11/28/2019
11/29/2019
I am attempting to convert these to standard date variables using this as.Date function:
as.Date(Dates, "%m/%d/%y")
I do not receive an error message, and it correctly interprets the month and date, but for some reason it's outputting the wrong year - one year ahead:
"2020-11-28"
"2020-11-29"
I have no earthly idea why it is incorrectly interpreting the year in this way. Any help is appreciated!
We need to use %Y for 4 digit year as %y refers to only 2 digit
as.Date(Dates, "%m/%d/%Y")
Or using lubridate, this would be resolved
library(lubridate)
mdy(Dates)
Or with anydate from anytime
library(anytime)
anydate(Dates)

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

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

Resources