'Object of type closure not subsettable when converting a monthly data series to daily using Tidyverse

I have the following series of inflation data:

library(tidyquant)
library(tidyverse)

symbol <- 'CPIAUCSL'
start_date <- as.Date('2022-01-01')
end_date <- as.Date('2022-03-31')

cpi_index <- tq_get(symbol,
                 from = start_date,
                 to = end_date,
                 get = 'economic.data')

cpi_index
  symbol   date       price
  <chr>    <date>     <dbl>
1 CPIAUCSL 2022-01-01  282.
2 CPIAUCSL 2022-02-01  284.
3 CPIAUCSL 2022-03-01  288.

I would like to expand this tibble into one that has daily data, as such:

  symbol   date       price
  <chr>    <date>     <dbl>
1 CPIAUCSL 2022-01-01  282.
2 CPIAUCSL 2022-01-02  282.
3 CPIAUCSL 2022-01-03  282.
4 CPIAUCSL 2022-01-04  282.
5 CPIAUCSL 2022-01-05  282.
... and so on

32 CPIAUCSL 2022-02-01  284.
33 CPIAUCSL 2022-02-02  284.
34 CPIAUCSL 2022-02-03  284.
34 CPIAUCSL 2022-02-04  284.
... and so on

86 CPIAUCSL 2022-03-28  288.
87 CPIAUCSL 2022-03-29  288.
88 CPIAUCSL 2022-03-30  288.
89 CPIAUCSL 2022-03-31  288.

To do this, I'm using the following:

cpi_index %>% mutate(date = ymd(date)) %>%
  group_by(date) %>%
  expand(date = seq(floor_date(date, unit = "month"),
                    ceiling_date(date, unit="month")-days(1), by="day"), price) %>%
  as.data.frame()

But, I'm seeing this error:

Error in `dplyr::summarise()`:
! Problem while computing `..1 = expand(data = dplyr::cur_data(), ..., .name_repair = .name_repair)`.
i The error occurred in group 1: date = 2022-01-01.
Caused by error in `object[[name, exact = TRUE]]`:
! object of type 'closure' is not subsettable
Run `rlang::last_error()` to see where the error occurred.

Here is the full error message:

> rlang::last_error()
<error/rlang_error>
Error in `dplyr::summarise()`:
! Problem while computing `..1 = expand(data = dplyr::cur_data(), ..., .name_repair = .name_repair)`.
i The error occurred in group 1: date = 2022-01-01.
Caused by error in `object[[name, exact = TRUE]]`:
! object of type 'closure' is not subsettable
---
Backtrace:
  1. ... %>% as.data.frame()
 18. lubridate::floor_date(date, unit = "month")
 20. stats::update.default(x, mdays = 1, hours = 0, minutes = 0, seconds = 0)
 22. stats:::getCall.default(object)
 23. base::getElement(x, "call")

Can anyone offer any assistance with this?

Thanks!



Solution 1:[1]

This doesn't seem reproducible on my machine, so at the OP's request I am posting a reprex here for demonstration only.

library(tidyquant)
#> Warning: package 'tidyquant' was built under R version 4.1.3
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
#> Loading required package: PerformanceAnalytics
#> Warning: package 'PerformanceAnalytics' was built under R version 4.1.3
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> 
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#> 
#>     legend
#> Loading required package: quantmod
#> Warning: package 'quantmod' was built under R version 4.1.3
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> == Need to Learn tidyquant? ====================================================
#> Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
#> </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 4.1.3

symbol <- 'CPIAUCSL'
start_date <- as.Date('2022-01-01')
end_date <- as.Date('2022-03-31')

cpi_index <- tq_get(symbol,
                 from = start_date,
                 to = end_date,
                 get = 'economic.data')

cpi_index %>% mutate(date = ymd(date)) %>%
  group_by(date) %>%
  expand(date = seq(floor_date(date, unit = "month"),
                    ceiling_date(date, unit="month")-days(1), by="day"), price) %>%
  as.data.frame()
#>          date   price
#> 1  2022-01-01 281.933
#> 2  2022-01-02 281.933
#> 3  2022-01-03 281.933
#> 4  2022-01-04 281.933
#> 5  2022-01-05 281.933
#> 6  2022-01-06 281.933
#> 7  2022-01-07 281.933
#> 8  2022-01-08 281.933
#> 9  2022-01-09 281.933
#> 10 2022-01-10 281.933
#> 11 2022-01-11 281.933
#> 12 2022-01-12 281.933
#> 13 2022-01-13 281.933
#> 14 2022-01-14 281.933
#> 15 2022-01-15 281.933
#> 16 2022-01-16 281.933
#> 17 2022-01-17 281.933
#> 18 2022-01-18 281.933
#> 19 2022-01-19 281.933
#> 20 2022-01-20 281.933
#> 21 2022-01-21 281.933
#> 22 2022-01-22 281.933
#> 23 2022-01-23 281.933
#> 24 2022-01-24 281.933
#> 25 2022-01-25 281.933
#> 26 2022-01-26 281.933
#> 27 2022-01-27 281.933
#> 28 2022-01-28 281.933
#> 29 2022-01-29 281.933
#> 30 2022-01-30 281.933
#> 31 2022-01-31 281.933
#> 32 2022-02-01 284.182
#> 33 2022-02-02 284.182
#> 34 2022-02-03 284.182
#> 35 2022-02-04 284.182
#> 36 2022-02-05 284.182
#> 37 2022-02-06 284.182
#> 38 2022-02-07 284.182
#> 39 2022-02-08 284.182
#> 40 2022-02-09 284.182
#> 41 2022-02-10 284.182
#> 42 2022-02-11 284.182
#> 43 2022-02-12 284.182
#> 44 2022-02-13 284.182
#> 45 2022-02-14 284.182
#> 46 2022-02-15 284.182
#> 47 2022-02-16 284.182
#> 48 2022-02-17 284.182
#> 49 2022-02-18 284.182
#> 50 2022-02-19 284.182
#> 51 2022-02-20 284.182
#> 52 2022-02-21 284.182
#> 53 2022-02-22 284.182
#> 54 2022-02-23 284.182
#> 55 2022-02-24 284.182
#> 56 2022-02-25 284.182
#> 57 2022-02-26 284.182
#> 58 2022-02-27 284.182
#> 59 2022-02-28 284.182
#> 60 2022-03-01 287.708
#> 61 2022-03-02 287.708
#> 62 2022-03-03 287.708
#> 63 2022-03-04 287.708
#> 64 2022-03-05 287.708
#> 65 2022-03-06 287.708
#> 66 2022-03-07 287.708
#> 67 2022-03-08 287.708
#> 68 2022-03-09 287.708
#> 69 2022-03-10 287.708
#> 70 2022-03-11 287.708
#> 71 2022-03-12 287.708
#> 72 2022-03-13 287.708
#> 73 2022-03-14 287.708
#> 74 2022-03-15 287.708
#> 75 2022-03-16 287.708
#> 76 2022-03-17 287.708
#> 77 2022-03-18 287.708
#> 78 2022-03-19 287.708
#> 79 2022-03-20 287.708
#> 80 2022-03-21 287.708
#> 81 2022-03-22 287.708
#> 82 2022-03-23 287.708
#> 83 2022-03-24 287.708
#> 84 2022-03-25 287.708
#> 85 2022-03-26 287.708
#> 86 2022-03-27 287.708
#> 87 2022-03-28 287.708
#> 88 2022-03-29 287.708
#> 89 2022-03-30 287.708
#> 90 2022-03-31 287.708

Created on 2022-04-23 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron