Turn character YYYY-Q1 into date [duplicate] - r

This question already has answers here:
Convert quarter/year format to a date
(1 answer)
Convert character field to quarter date in R
(3 answers)
Closed 2 years ago.
I would like to turn a character variable formatted as YYYY-QQ, so for example 2010-Q1, into something I can use as a date. I have played around with as.yearqtr, but haven't could only figure out the output format. I haven't been been able to define the input format though.
Edit:
Has also been answered here.

You have to use the right format in as.yearqtr formula like this:
library(zoo)
x<-"2010-Q1"
as.yearqtr(x, format = "%Y-Q%q")
[1] "2010 Q1"

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)

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
>

Converting string formatted date to as.Date dd/mm/yyyy to dd-mm-yyyy in r [duplicate]

This question already has answers here:
Convert date-time string to class Date
(4 answers)
Convert string to date, format: "dd.mm.yyyy"
(4 answers)
Closed 2 years ago.
still fairly new to r. I have searched through the forum on different solutions that don't work on the data set that i have and what output i'm trying to achieve.
I have a column containing dates in the format dd/mm/yyyy (WorkoutDay) and would like to format it to dd-mm-yyyy.
running as.date function through the column gives me this output:
## example date in on of the columns "20/03/2020"
rd$WokoutDay <- as.Date(rd$WorkoutDay)
##output 0031-03-20
I tried to run a format function through but I get this instead which is also not what I'm after.
rd$WorkoutDay <- as.Date(rd$WorkoutDay) %>%
format("%d-%m-%y")
## output 20-03-31
I've read that maybe strptime or as.POSIXct might help but not sure how to use them
thanks in advance :)

How to convert mmm-yy(Jan-85) date format into yyyy-mm--dd (1985-01-02)date format in R? [duplicate]

This question already has an answer here:
format year-month to POSIXct [duplicate]
(1 answer)
Closed 3 years ago.
I want to convert mmm-yy(Jan-85) date format into yyyy-mm-dd (1985-01-02)date format in R.
One option is to paste the day and convert it to Date class with as.Date and the appropriate format
as.Date(paste0("Jan-85", "-02"), "%b-%y-%d")
#[1] "1985-01-02"

how to convert unarranged dates to the default date format in R [duplicate]

This question already has answers here:
Convert numeric to date
(2 answers)
Closed 6 years ago.
I'm working with hundreds of stores data with R.
And there is store open data.
But it's unarranged, for example "20061019".
I'd like to convert into "YYYY-MM-DD" format.
Plz teach me how to deal with it. thx :)
paste(substr(aa,1,4),"")
[1] "2006 " "2016 "
paste0(substr(aa,1,4),"-",substr(aa,5,6),"-",substr(aa,7,8))
[1] "2006-10-19" "2016-09-09"
We can use as.Date with format argument (from base R)
as.Date("20061019", format = "%Y%m%d")
#[1] "2006-10-19"
One of the ways you can do this is with lubridate ymd function like this
library(lubridate)
ymd("20061019")

Resources