Convert integer data into year and month [duplicate] - r

This question already has answers here:
Convert numeric date format (YYYYMMDD; 20150101) into Year-Month
(3 answers)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I have integer data example like this
201901, 201912, 202004
How to convert my data into year and month using R Programming. I need output like this:
2019-01, 2019-12, 2020-04
or
Jan-2019, Dec-2019, Apr-2020
Thank you in advance.

You can convert to date and then use format :
x <- c(201901, 201912, 202004)
format(as.Date(paste(x, '01'), '%Y%m%d'), '%b-%Y')
#[1] "Jan-2019" "Dec-2019" "Apr-2020"
format(as.Date(paste(x, '01'), '%Y%m%d'), '%Y-%m')
#[1] "2019-01" "2019-12" "2020-04"
You can also use zoo's yearmon :
zoo::as.yearmon(as.character(x), '%Y%m')
Using regex we can capture 4 and 2 characters and insert a "-" in between.
sub('(.{4})(.{2})', '\\1-\\2', x)

Related

Convert character into date format [duplicate]

This question already has answers here:
Convert a yyyymm format to date in R? [duplicate]
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 8 months ago.
I currently have a database with dates in character format yyyy-mm format. e.g. 2009-01 and missing values.
I'm struggling to convert this into a date format.
You could use ym from lubridate package:
lubridate::ym('2009-01')
#[1] "2009-01-01"

Convert character date to date format [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I have date in character vector. I need to convert it to date format.
For example :
Month = "202005"
Output should be May-20. It should be in date format so that I can use to filter some dates. Similarly 201912 should return Dec-19
zoo has a function called yearmon().
library(zoo)
month <- "202005"
as.yearmon(month, "%Y%m")
# "May 2020"

Sort by Month and Year in Dataframe [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
R converting a factor YYYY-MM to a date
(2 answers)
Sort year-month column by year AND month
(1 answer)
Closed 4 years ago.
I'm having trouble sorting by month/year format in R.
I have data in a %m/%Y format but in trying to use
df_prod<-df_prod[order(as.Date(df_prod$date,format="%m/%Y")),]
The data frame is not sorting. I'm getting a text-like ordering (01/2000,01/2001,01/2002)
Additional details:
I'm performing an dplyr aggregation on a dataframe that was nicely sorted in %m/%Y format:
df_prod<-df %>%
group_by(date,comp_id) %>%
summarise(a_prod=prod(1+TRT)-1)
Thank you
As #lmo has already mentioned that using just %m/%Y will not create a valid date. One solution is to use as.yearmon from zoo package.
library(zoo)
df_prod<-df_prod[order(as.yearmon(df_prod$date,format="%m/%Y")),]
%m/%Y is a tricky starting format for a date, if that's what you ultimately need:
grab date components from the string:
aa <- format(Sys.Date(), "%m/%Y")
m <- as.numeric(substr(aa, 1, 2))
y <- as.numeric(substr(aa, 4, 8))
Then you can paste them together into a date format and sort:
as.Date(paste(y, "-", m, "-01", sep = ''))

Get the year from a timestamp in R [duplicate]

This question already has answers here:
Extract month and year from a zoo::yearmon object
(7 answers)
Closed 7 years ago.
I have an r dataset which has a list of timestamps like this: 2009-08-18 14:38:20 2010-08-10 14:58:25 etc
I want to extract the year but the Posixct doesn't have a years function unlike the months(t$timestamp)
Is there a way to get only 2009?
Use format:
x <- as.POSIXct(c("2009-08-18 14:38:20", "2010-08-10 14:58:25"))
format(x, "%Y")
Have you tried ?
strptime(yourtimestamp, "%Y")

Converting dates with a format of d/mm/yy (for years earlier than 69) [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 9 years ago.
Converting the character "6/07/69" into a date using the as.Date function results in "2068-07-06" instead of "1968-07-06". How can I fix this?
Example:
as.Date(c("6/07/68", "6/07/69"), format="%d/%m/%y")
[1] "2068-07-06" "1969-07-06"
you can use library chron
e.g.
> library(chron)
> as.Date(chron(c("6/07/67", "6/07/69"), format = c(dates = "m/d/y")))
#[1] "1967-06-07" "1969-06-07"

Resources