Search code examples
rdataframefiltersubsetquantmod

write a function that can retrieve only n period of data in R


Working with a set of sample data like below:

library(quantmod)

ticker<-"AAPL"
start_date <- as.Date("2020-01-01")
end_date <- as.Date("2021-01-01")
getSymbols("AAPL", from=start_date, to=end_date)

data<-AAPL$AAPL.Adjusted

Say if I only need the last 10 or 20 columns and put into a subset, instead of using below line:

n=10

new_data<-tail(data,n=n)
    
 > new_data
           AAPL.Adjusted
2021-01-15      125.2671
2021-01-19      125.9469
2021-01-20      130.0850
2021-01-21      134.8537
2021-01-22      137.0213
2021-01-25      140.8146
2021-01-26      141.0511
2021-01-27      139.9673
2021-01-28      135.0705
2021-01-29      130.0161

Is there a better way to retrieve data from the data set containing "n" period only? Thanks.


Solution

  • The function Bubbles is looking for is this:

    previous_n_days<- function(d, n){
        d %>% tail(n) %>%
        head(n-1)
        }
    # this assumes data goes right up until today
    

    Or, if subset() is required, this:

    previous_n_days <- function(d, n){
        # check if it's in the last n days using subset
        subset(d, between(index(d), Sys.Date() - n - 1, Sys.Date() -1))
    }