I have a dataframe where the first column is a date in d/m/y format and the second is a numeric value (sales).
I want to create subsets for each month of one year (eg. 11/11, 12/11 etc). I tried the code suggested in this answer: subset a data.frame with multiple conditions
and it works when the condition on the month is imposed:
subset(sales, format.Date(date, "%m")=="11")
but it returns an empty subset with error message invalid 'x' argument
when I add the year condition:
subset(sales, format.Date(date, "%m")=="11" & format.Date(date, "%y")=="11")
I am using R 2.10.1-2 on Ubuntu 10.04, thanks for the help you can give.
The "Y" is case sensitive in dates. I don't know why, but the "m" for months and the "d" for days are lower case, but the "Y" must be upper case. This should work for you:
Since you didn't provide a data set I made my own from the link you provided. Your method works for me and I get an empty data set only if I don't meet both of the conditions supplied (month and year) so I'm guessing you're you're attempting to subset a date series (month and year) that doesn't exist (but can't tell for certain without the code you're using). Here's the code I used:
This answer avoids
subset
, handles missing observations and usesas.POSIXct
date/time format. Although, the rest of the code is virtually the same as in Tyler Rinker's answer. Note that I have to specify the name of the date/time variable insideas.POSIXct
rather than using the name of the unformatted variableDate_Time
.