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')
Related
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
>
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
This question already has answers here:
How do I convert date to number of days in R
(7 answers)
Closed 4 years ago.
My question doesn't have to do with my own dataset, but since I'm new to R, I wanted to make sure I knew how to work with dates, so I'm searching up the different ways to manipulate and compare dates in R.
I recently read an answer to a question regarding converting a date into an integer date using the as.numeric () function. Here is the answer that was accepted: https://stackoverflow.com/a/8215581/10864249
So from that answer, my understanding is that the date was converted into seconds.
Why would anyone want to use the as.numeric() function if we're going to only get seconds?
Can we convert the integer date into a smaller integer, like # of days by just dividing by 365.25 or by months even by dividing by 12, then? I assume it'd be easier to compare dates that way, rather than in seconds.
Thanks!!
coercing a date object into a numeric object will give you "the number of days since 01/01/1970"
my_date = as.Date('2015-01-01')
my_date
#[1] "2015-01-01"
class(my_date)
# [1] "Date"
as.numeric(my_date)
# [1] 16436
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
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")