-2

I have the following data.frame:

           Bounty Snickers Mars
2014-10-02    400      200 1000
2014-10-03    400      100  100
2014-10-04    100      100  500
2014-10-05    800      900  600
2014-10-06    400      300  400
2014-10-07   2000     1500  800
       ...    ...      ...  ...

Is there a way to select all rows that fall, for example, on a Monday, or on a weekend?

Philip Seyfi
  • 929
  • 1
  • 10
  • 24
  • 1
    Take a look [**here**](http://stackoverflow.com/questions/6009351/removing-weekend-data-in-an-r-dataframe) for weekends and [**here**](http://stackoverflow.com/questions/9216138/find-the-day-of-a-week-in-r) for week days. Next time try to use Google first, thanks. – David Arenburg Nov 27 '14 at 12:20
  • 1
    @DavidArenburg I've Googled for 30min before posting the question using terms such as "R subset by weekday", "R select data on weekend", "R data frame select weekends", etc. – Philip Seyfi Nov 27 '14 at 12:31

1 Answers1

1

Try

df[format(as.Date(row.names(df)), '%A')=='Monday',]
#         Bounty Snickers Mars
#2014-10-06    400      300  400

Or for the weekend one option is to use wday

 library(lubridate)
 df[wday(as.Date(row.names(df))) %in% 6:7,]
 #        Bounty Snickers Mars
 #2014-10-03    400      100  100
 #2014-10-04    100      100  500
akrun
  • 874,273
  • 37
  • 540
  • 662