Convert date field to Quarter in R - r

I have data frame which has the date column in format 08/21/2000(m/d/Y).
Now I want to add new column which display the date in Quarter format that is 2000-Q3.
I install zoo package and used following command. but it is giving NA value.
library(zoo)
mydf$var9=as.yearqtr("mydf$Order.Date","%m/%d/%Y").

The zoo library has created a set of functions to handle year-quarter vectors:
library(zoo)
mydf$var9=as.yearqtr(as.Date( mydf$Order.Date, "%m/%d/%Y" ).

Related

How to extract Month and Year from Date column and add two more columns for each

I am currently working on a data frame, which is imported from a .csv file that has a "Date" column:
First Image
Then I wanted to extract the month and year from the "Date" column into two new columns respectively for month and year with the following code I made:
act_weather_data["Month"] <- format(as.Date(act_weather_data$Date), "%m") ## For Month
act_weather_data["Year"] <- format(as.Date(act_weather_data$Date), "%Y") ## For Year
The above code worked however, the Year column seems to be displayed incorrectly:
Second Image
It appears that the Year column is using the date but not the actual year that can be seen from the "Date" column.
I'm not sure as to why the "Year" column appears like this. Would anyone be able to help me with this? Thanks a lot!
For the solution below it is necessary to install two packages: dplyr and lubridate.
# Install the necessary packages
install.packages("dplyr")
install.packages("lubridate")
# Load the packages
library(dplyr)
library(lubridate)
# create dates dataframe
dates_dt <- data.frame(the_dates=seq(as.Date('2022-01-01'),
as.Date('2022-01-10'),
by='days'))
# Look at the dataframe
dates_dt
# Double check they are actually dates
class(dates_dt$the_dates)
# Extract the month
lubridate::month(dates_dt$the_dates)
# Extract the year
lubridate::year(dates_dt$the_dates)
# Perhaps you want the month name instead? no problem
month.name[lubridate::month(dates_dt$the_dates)]
# Now add a column for each
dates_dt <- dates_dt %>%
mutate(year=lubridate::year(dates_dt$the_dates),
month=lubridate::month(dates_dt$the_dates),
month_name=month.name[lubridate::month(dates_dt$the_dates)])
# Have a look at the result
dates_dt
I hope you find it useful. Happy coding in R!
Looks like it is grabbing the day instead of the year. So would seem that your date isn't properly formatted when it goes through the as.Date() function
See what this
as.Date(act_weather_data$Date)
looks like on its own and format accordingly
i.e.
as.Date(act_weather_data$Date, format="%Y/%m/%d")
Then apply.the formatting as before
i.e
Year=format(as.Date(act_weather_data$Date, format="%Y/%m/%d"),"%Y")

I can't convert an unknown column in to a date column and then change format to dd-mm-yyyy

I know it seems that this may be a repeated question, but I have tried other solutions and still cannot get it to work. I have uploaded a .csv file into r. I have done a small amount of house cleaning but ultimately I would like to convert a column from '"POSIXct" "POSIXt"' to a 'date' column type, and a 'character' column to a 'numeric'. For the latter column (change) I have decimals and --- entries, I converted the --- to NA, but fail to convert it to a 'numeric' afterwards.
df$value <- as.numeric(as.character(df$value))
I first used:
df$date <- dmy_hm(df$time_stamp, tz = "Europe/London")
to create a new date variable / column. But this did not give 'date' as a column type. I then tried using:
df$date <- as.Date(df$date)
but this did not work. Once I have converted to 'date' I need to convert the format from yyyy-mm-dd hh:mm:ss to dd/mm/yyyy.
Any help with will greatly received.
lubridate package can be wacky sometimes. Can you share head of you .csv data? you might have confused with dmy_hms with myd_hms or ymd_hms formats. Try using anytime package.
anytime::anytime(df$time_stamp)

split dates column in R when there are no sperators

I have a data set with date column with following format i.e. 19700101
How can I convert it as 1970-01-01 format
I tried zoo package in R but could not work it out. Can any one help me on this
library(anytime)
anydate(19700101)

Quarterly Date in a Data Frame

I have a dataframe that is structured as such
Date Value Variable
However, the date in my data is quarterly, read in from a .csv in the form 2000 Q1, etc.
As such, my normal method of converting dates:
mychart11$Date <- as.Date(mychart$Date , "%d/%m/%y")
mychart11[order(mychart$Date),]
does not work because there is no %q option for quarterly.
I'm aware I could create quarterly dates from the data using the zoo package, but I need them to be in the dataframe, because I need to plot the dates using ggplot later using code like
geom_line(data=mychart, aes(Date, value, group="Date", colour="Total"),
position=position_dodge(1)) + scale_x_date(labels = date_format("%b"))
and because the date is a factor, I get the error message
Invalid input: date_trans works with objects of class Date only
Any help is much appreciated.
You can use zoo to convert your data to class yearqtr and then use as.Date() (from the zoo package...it is actually as.Date.yearqtr()) to convert to a regular date.
mychart11$Date <- as.Date(as.yearqtr(mychar$Date),frac=0)
Notice that frac=0 will create a resulting date that is at the beginning of said quarter. Use frac=1 to get the end of the quarter. In between values are also permitted, though I can't think they would be common.

Change factor to a date format in R

I have a file with dates in the format YYYY-mm. I am trying to prepare my data for a time series analysis and therefore need to convert the formats from factor to a Date format.
What I've tried:
x <- '2011-11'
as.Date(as.character(a), "%Y-%m")
the last line gives me an output NA.
Thank you!
"2011-11" is not a date (where's the day)? Either add a day:
a <- '2011-11'
as.Date(paste0(a,'-01'))
or use zoo's yearmon class:
library(zoo)
as.yearmon(a)
'2011-11' isn't a date. Use paste(your.year.month.strs, '01', sep='-') to add the day component to your strings, then call as.Date with "%Y-%m-%d".

Resources