I need a monthly Frequency

Hi,
I'm doing a project by r.
i need to convert a data that i downloaded with r from daily to monthly:

first.date<-as.Date("2002-01-02")
last.date<-as.Date("2020-10-30")

tickers = c("^GSPC")
l.out = BatchGetSymbols(tickers = tickers,
first.date = first.date, last.date = last.date,
cache.folder = file.path(tempdir(),"BGS_Cache"))
View(l.out$df.tickers)
How can i convert in monthly deadlines?

Hi Vittoriodale, welcome to RStudio Community.

It's hard for us to help since we cannot reproduce your data. To facilitate this, you need to provide a reproducible example (or reprex for short). You can learn how to make one by following this guide.

Thanks siddharthprabhu for your help.
My problem is not a error. I downloaded a data in days from 2002 Gen to 2020 october, i want to download a monthly frequency in the same period.
For example i used this code:
library(readxl)
library(BatchGetSymbols)
library(rvest)
library(xml2)
library(quantmod)
library(foreign)
library(xts)
library(ggplot2)
library(rsq)
library(pastecs)

first.date<-as.Date("2002-01-02")
last.date<-as.Date("2020-10-30")

#S&P500.rm
tickers = c("^GSPC")
l.out = BatchGetSymbols(tickers = tickers,
first.date = first.date, last.date = last.date,
cache.folder = file.path(tempdir(),"BGS_Cache"))
View(l.out$df.tickers)

this have a daily frequency i need a monthly frequency.
I hope you understand, sorry for my english.

Reprexes are not just for errors. They allow others to reconstruct a sample of your data. That is very handy when it comes to figuring out an appropriate solution.

Since I don't know what your data looks like, I'll make up some of my own and illustrate how to aggregate it. You can adapt it to your needs.

library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)

df <- tibble::tribble(~date, ~value,
                      "2002-01-02", 20,
                      "2002-01-05", 50,
                      "2002-01-07", 35,
                      "2002-02-03", 10,
                      "2002-02-08", 70,
                      "2003-01-16", 10,
                      "2003-01-25", 90) %>% 
  mutate(date = as.Date(date))

df %>% 
  group_by(year = year(date), month = month(date, label = TRUE)) %>% 
  summarise(total_value = sum(value))
#> `summarise()` regrouping output by 'year' (override with `.groups` argument)
#> # A tibble: 3 x 3
#> # Groups:   year [2]
#>    year month total_value
#>   <dbl> <ord>       <dbl>
#> 1  2002 Jan           105
#> 2  2002 Feb            80
#> 3  2003 Jan           100

Created on 2020-11-20 by the reprex package (v0.3.0)

I should use the first day of every month from 2002 to 2020.
2002-01-01
2002-02-01
2002-03-01
...
...
2020-08-01
2020-09-01
2020-10-01

Sorry, I didn't understand your intent. Do you want to show the first value of each month or do you want to aggregate?

I want to show the first value of each month.

Thanks for you support and sorry for my bad english!!

No worries. You could use dplyr::slice_min() to do that.

library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)

df <- tibble::tribble(~date, ~value,
                      "2002-01-02", 20,
                      "2002-01-05", 50,
                      "2002-01-07", 35,
                      "2002-02-03", 10,
                      "2002-02-08", 70,
                      "2003-01-16", 10,
                      "2003-01-25", 90) %>% 
  mutate(date = as.Date(date))

df %>% 
  group_by(year = year(date), month = month(date, label = TRUE)) %>% 
  slice_min(date)
#> # A tibble: 3 x 4
#> # Groups:   year, month [3]
#>   date       value  year month
#>   <date>     <dbl> <dbl> <ord>
#> 1 2002-01-02    20  2002 Jan  
#> 2 2002-02-03    10  2002 Feb  
#> 3 2003-01-16    10  2003 Jan

Created on 2020-11-20 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.