0

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.

Ned
  • 3
  • 2
  • 1
    `&&` operator examines only the first element. Use `&`. http://stackoverflow.com/questions/6933598/r-gotcha-logical-and-operator-for-combining-conditions-is-not – bergant Apr 09 '15 at 00:44

1 Answers1

1

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 ||.

Community
  • 1
  • 1
Ian Fiske
  • 9,752
  • 3
  • 18
  • 18
  • I find it intriguing that you can compare a `Date` object to a string and it works just fine. Never would have thought that would work. – thelatemail Apr 09 '15 at 00:46
  • @thelatemail what about `1 < "2"` ? – bergant Apr 09 '15 at 00:47
  • @bergant - I know that works, but it just seems risky, as in `1 < "02"` , which would be much safer with `1 < as.numeric("02")`. I notice the `Date-to-string` comparison fails if the date is not in an unambiguous, standard format, so I guess that's okay. Though it is still a risky practice, in that doing `Sys.time() < "1900-01-01 00:00"` returns `TRUE`, so the pattern doesn't hold when dealing with all date/datetime objects. – thelatemail Apr 09 '15 at 00:48
  • so essentially, I could remove the `& starbucks[,7] <= today` part and it would've worked. – Ned Apr 09 '15 at 01:05