mercredi 8 juillet 2015

Using logical expressions with dates in R

I have been working on flagging certain records in R based on whether or not they're more recent than yesterday, but I've either been returning only zeros or getting an error.

I have one dataframe that lists all of my stores. I have another dataframe that lists all of their store level sales records (usually trailing 30 days reporting up to yesterday). I receive the sales report daily. I want to put a flag in the list of stores to indicate whether I received a sales report record for the store yesterday.

To create the date, I used this code (using the lubridate package):

today <- as.Date(today(), format= "%m/%d/%Y")
yesterday <- today-1

Then I used a if statement to iterate through the code:

for(i in 1:length(storelist[,1])){
  if ((storelist$Store_NO[i] %in% storesales$Store_No) && (storesales$Calendar.Date == yesterday))
    (storelist$Flag[i] <- 1)
    else (storelist$Flag[i] <- 0)
}

NB: The date in the storesales dataframe is in m/d/y, but I wasn't sure whether this would have an impact.

However, when I do this, all of the 'flag' column are populated with zeros, even though I know for a fact that at least one or two reported sales yesterday.

How can I fix this? In excel, this would be a pretty straight forward SUMIF (where the first criteria would be the store no and the second criteria would be the date), but I can't get this to translate to R.

Thanks for your help everybody!

Aucun commentaire:

Enregistrer un commentaire