Conversion of monthly and quarterly data into annual in R

Viewed 661

Hi I am trying to learn to use FRED Data to convert monthly and quarterly into annual data. My data looks like this:

   Date FEDFUNDS
7/1/2000    6.54
8/1/2000    6.5
9/1/2000    6.52
10/1/2000   6.51
11/1/2000   6.51
12/1/2000   6.4

Previously, seeing other posts I tried using the following to convert monthly to quarterly.

library(tidyverse)
data<- read_csv(file.choose())
head(data)
data$Date=as.Date(data$Date, formate="$m/$d/%y")
data<-arrange(data,Date)
library(zoo)
data$qdate<-as.yearqtr(data$Date)
library(date)
data_qrtly<-data %>%
  group_by(qdate) %>%
  summarise_all(mean)

But I'm having difficult in trying the "DF" in converting monthly or quarterly to yearly data. Any help will be very appreciated.

1 Answers

How do you want to summarize the data? I assume you want the mean of FEDFUNDS by year. This is straightforward to do in the tidyverse:

library(tidyverse)
library(lubridate)

test_data <- tribble(~Date, ~FEDFUNDS,
                     "7/1/2000", 6.54,
                     "8/1/2000", 6.5,
                     "9/1/2000", 6.52,
                     "10/1/2000", 6.51,
                     "11/1/2000", 6.51,
                     "12/1/2000", 6.4)

#convert the date into a date type column
#create a new "year" column
test_data %>% 
  mutate(Date = lubridate::mdy(Date),
         year = lubridate::year(Date)) %>% 
  group_by(year) %>% 
  summarize(fedfunds_mean = mean(FEDFUNDS))

# A tibble: 1 x 2
year fedfunds_mean
<dbl>         <dbl>
2000          6.50
Related