Afke
Afke

Reputation: 989

R dataframe - adding new column with starting date of year quarter

I have a dataframe like:

yq       store   value    
2014 Q1  1000    89

How can I add an extra column which contains, based on the year quarter in column 1 , the starting date of that quarter. So in this example I would like to add: 01-01-2014.

Upvotes: 2

Views: 770

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 270348

Assuming that the data is as in the Note at the end, first convert yq to a yearqtr object. Such objects are represented internally as year + frac where frac = 0, 1/4, 2/4 and 3/4 for Q1, Q2, Q3 and Q4. They render as shown below and can be readily manipulated, e.g. yq + 1/4 is the next quarter. See ?yearqtr for more information.

Given the yearqtr object you may not even need the Date column; however, if you do then convert that to Date class. The default conversion is to give the first of the quarter. (Use the frac=1 argument of as.Date to get the end of the quarter.)

library(zoo)
transform(transform(DF, yq = as.yearqtr(yq)), Date = as.Date(yq))

giving:

       yq store value       Date
1 2014 Q1  1000    89 2014-01-01

If you don't need yq as a yearqtr object then it could be shortened slightly:

transform(DF, Date = as.Date(as.yearqtr(yq)))

Note

The input shown reproducibly is assumed to be:

DF <- structure(list(yq = structure(1L, .Label = "2014 Q1", class = "factor"), 
    store = 1000L, value = 89L), class = "data.frame", row.names = c(NA, 
-1L))

Upvotes: 0

Konrad
Konrad

Reputation: 18657

For instance, you could use lubridate:

library(tidyverse)
df <- tibble(yq = "2014 Q1", store = 1e3, value = 89)

df_new <- df %>% 
    mutate(start_date = lubridate::yq(yq))

Results

# A tibble: 1 x 4
  yq      store value start_date
  <chr>   <dbl> <dbl> <date>    
1 2014 Q1  1000    89 2014-01-01

Upvotes: 2

Related Questions