When I try to make a new column in r for each date to indicate its quarter it only gives me one response [duplicate] - r

This question already has answers here:
Format date as Year/Quarter
(10 answers)
Closed 2 years ago.
In one of my columns I am given a date. I am trying to make a new column to indicate which quarter that date lies in so that way i can determine how many observations entered a certain venue in each quarter. End goal is to sumarize based off of quarter. Here is an example of the code I am using and an example of the output. as.Date(as.yearqtr(x, format ="%Y-%m-%d" )) . As you can see my problem is that It only returns 2019 q2, and i am confused why. This is my code PARTIES$QUARTER <- (as.yearqtr(PARTIES$opened, format = "%Y-%M-%d"))

It should be %m for (month) and not %M (which specifies the Minute as decimal number (00–59).)
library(zoo)
PARTIES$QUARTER <- as.yearqtr(PARTIES$opened, format = "%Y-%m-%d")

Related

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"))

%b-%Y date conversion gives NA [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.

Why does R impute the 12th of the month when formatting a year as a Date [duplicate]

This question already has answers here:
Convert four digit year values to class Date
(5 answers)
Closed 5 years ago.
I note in R if one calls as.Date(as.character(2002:2013), format='%Y') the output is
[1] "2002-01-12" "2003-01-12" "2004-01-12" ...
I would like R to give me the first of the month instead. I could supply the whole date, paste(2002, '01', '01', sep='-'), but am curious why the year-only format imputes the 12th of the month and also to see other solutions.
Ah, just found my answer: The missing sections of the Date object (month/day) are imputed from today's date (System Date).

How to add a column and convert dates in R [duplicate]

This question already has an answer here:
How can I convert this to month?
(1 answer)
Closed 8 years ago.
So I have the following data set which shows temperatures for 365 days of the year. Each date is labeled by the day of the week in a y-m-d format. I'm trying to add a new column called month, which will display the current month which the date shows. Ex: 2014-01-01 will show January while 2014-10-25 will show October. Can anyone help? I'm trying to use Lubridate, but I'm still new at R, and am having a lot of trouble.
The format.Date function return a vector of character values and the "%B" parameter determine the result as full month values:
dfrm$Month <- format( as.Date(dfrm$yourDate) , "%B")
If yourDate-column were already an R Date-object then the as.Date is not needed. Consult the help page for strptime. There can be separators in between the (possibly multiple) format specs
?strptime

Resources