Ned
Ned

Reputation: 3

Subsetting my data frame by Date

I have seen the code somewhere, but haven't found it in the 30 mins I've been searching.

Here's the code I have for now

library('quantmod')
today <- Sys.Date()
getSymbols("SBUX")
retSBUX <- dailyReturn(SBUX)
starbucks <- data.frame(SBUX)
starbucks[,7] <- as.Date(row.names(starbucks))
row.names(starbucks) <- NULL
starbucks <- subset(starbucks, starbucks[,7] >= "2015-04-06" && starbucks[,7] <= today)

When I run this code, I get a data frame with 0 variable and just the column names in the data frame.

Upvotes: 0

Views: 71

Answers (1)

Ian Fiske
Ian Fiske

Reputation: 10612

You should used the vectorized logical operator & instead of the short-circuit operator (&&):

starbucks <- subset(starbucks, starbucks[,7] >= "2015-04-06" & starbucks[,7] <= today)

See R - boolean operators && and ||.

Upvotes: 1

Related Questions