-1

I am working on a project and would be happy about your help.

I am working with stocks and the effect of weekdays on performance, is there a way to take all the values (for instance the S&P 500) of a data frame (df) from a specific weekday (e.g. Tuesday) and enter these values in a different data frame (df2) in a new column?

Thank you very much,

Ferdinand

  • Can you share a sample of your data frame? – Aziz Jul 10 '20 at 21:36
  • The "duplicate", which has no accepted answer, asks about "POSIXct" and "lubridate", this is not necessarily what the OP is asking here ... – Trusky Jul 10 '20 at 21:49
  • @Trusky The code is there `weekdays(as.Date(D$Time)); subset(D, D$Weekday == "Tuesday")`. It doesn't need to have an accepted answer – akrun Jul 10 '20 at 23:29

1 Answers1

1
df <- read.csv("AAPL.csv")  # from Yahoo! Finance
  > head(df)                                                          
          Date   Open   High    Low  Close Adj.Close   Volume         
  1 2019-07-10 201.85 203.73 201.56 203.23  200.8332 17897100         
  2 2019-07-11 203.31 204.39 201.71 201.75  199.3706 20191800         
  3 2019-07-12 202.45 204.00 202.20 203.30  200.9023 17595200         
  4 2019-07-15 204.09 205.87 204.00 205.21  202.7898 16947400         
  5 2019-07-16 204.59 206.11 203.50 204.50  202.0882 16866800         
  6 2019-07-17 204.05 205.09 203.27 203.35  200.9517 14107500
df$Day <- format(as.Date(df$Date), "%A")  # Get the day
idx <- df$Day == "Tuesday"  # Where are the Tuesdays ?
df2 <- df[idx, ]  # Logical indexing
  > head(df2)
           Date   Open   High    Low  Close Adj.Close   Volume     Day
  5  2019-07-16 204.59 206.11 203.50 204.50  202.0882 16866800 Tuesday
  10 2019-07-23 208.46 208.91 207.29 208.84  206.3770 18355200 Tuesday
  15 2019-07-30 208.76 210.16 207.31 208.78  206.3177 33935700 Tuesday
  20 2019-08-06 196.31 198.07 194.04 197.00  194.6766 35824800 Tuesday
  25 2019-08-13 201.02 212.14 200.48 208.97  207.2901 47218500 Tuesday
  30 2019-08-20 210.88 213.35 210.32 210.36  208.6689 26884300 Tuesday

Your function :

myfunction <- function(mydf) {                                                  
  df$Day <- format(as.Date(df$Date), "%A")                                                                          
  idx <- df$Day == "Tuesday"                                                                                        
  df2 <- df[idx, ]                                                                                                  
}

Testing myfunction :

  > out = myfunction(df)                                              
  > head(out)                                                         
           Date   Open   High    Low  Close Adj.Close   Volume     Day
  5  2019-07-16 204.59 206.11 203.50 204.50  202.0882 16866800 Tuesday
  10 2019-07-23 208.46 208.91 207.29 208.84  206.3770 18355200 Tuesday
  15 2019-07-30 208.76 210.16 207.31 208.78  206.3177 33935700 Tuesday
  20 2019-08-06 196.31 198.07 194.04 197.00  194.6766 35824800 Tuesday
  25 2019-08-13 201.02 212.14 200.48 208.97  207.2901 47218500 Tuesday
  30 2019-08-20 210.88 213.35 210.32 210.36  208.6689 26884300 Tuesday
Trusky
  • 483
  • 2
  • 13