3

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.

Mark
  • 7,785
  • 2
  • 14
  • 34
Bubbles
  • 455
  • 2
  • 8
  • 1
    `subset(data, seq(nrow(data))>=nrow(data) - 10 + 1)` but why not just use `tail(data, n)`?? – Onyambu Jun 27 '23 at 21:46
  • 1
    @Onyambu I think they want to subset before it is downloaded into R. – zx8754 Jun 27 '23 at 21:49
  • 1
    Why not just set `from` to `to - n days` ? – zx8754 Jun 27 '23 at 21:50
  • 1
    in what ways are you wanting the new method to be "better"? Because `tail()` seems ideal – Mark Jun 27 '23 at 21:56
  • @onyambu, that's exactly what I want , sorry my post is not very clear. I was also thinking, if I can add a filter like x=today-1, data collecting from x-n , so say if n=10, the data downloaded will be showing date x-1 to x-11. Cheers. – Bubbles Jun 27 '23 at 22:02

1 Answers1

2

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))
}
Mark
  • 7,785
  • 2
  • 14
  • 34