Sort by Month and Year in Dataframe [duplicate] - r

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 = ''))

Related

Convert month-year to date format in r [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 1 year ago.
I have a date that is in the this format:
chr [1:56] "Sep-2016" "Oct-2016" "Nov-2016" "Dec-2016" "Jan-2017" "Feb-2017" "Mar-2017" "Apr-2017"
I tried as.Date(Dates, "%b-%Y") and got NA for all the values. For some reason I have tried multiple ways and using different format but it is still not working. I am looking to get it into either 09-2016, 10-2016 or just simply turn it into a date format.
Any help is much appreciated!
Date class needs a day as well. Easiest is to convert to yearmon class from zoo and then coerce it to Date, which adds a dummy day
library(zoo)
as.Date(as.yearmon(Dates, '%b-%Y'))
or in base R, paste a day and convert
as.Date(paste0(Dates, '-01'), '%b-%Y-%d')

Convert integer data into year and month [duplicate]

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)

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
>

Using as.Date function in R on columns with blank cells and dates [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 3 years ago.
I have encountered a difficulty, trying to use the as.Date function (in R) on a data frame to preserve date format. The date column consists of blank cells (i.e. missing dates) and observed dates in the format month/year (e.g. 8/2019).
As mentioned earlier, I have tried using the as.Date function but the column for the dates turns blank completely (i.e. no dates are reported). Below is the code I am using:
df$date <- df$date<- as.Date(df$date, format='%m/%Y') #df is the data frame
The expected results should have the observed dates and the missing dates replaced with NA. I greatly appreciate your help.
You need to add a date component to make it a complete date. Once you do that it is easy to convert it into an actual date object
as.Date(paste0("1/", "8/2019"), "%d/%m/%Y")
#[1] "2019-08-01"
Or using dmy from lubridate
lubridate::dmy(paste0("1/", "8/2019"))

How to convert julian date into standard date for different years? [duplicate]

This question already has answers here:
Convert Julian date to calendar dates within a data frame
(3 answers)
Closed 2 years ago.
I have julian dates for several years, I want to convert them to a standard date format (year-month-day). I tried with the following code
library(epitools)
library(dplyr)
df$new_date <- df %>%
group_by(year) %>%
mutate(new_date = julian2date(julian_date))
but it get the following message:
Error: do not know how to convert 'attributes(x)[[1]]' to class “Date”
The julian2date() function is not needed. The following code will convert the julian_date variable to a standard date format.
as.Date(julian_date, origin="1970-01-01")

Resources