A Trask
A Trask

Reputation: 13

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!

Upvotes: 1

Views: 4096

Answers (2)

Pierre L
Pierre L

Reputation: 28441

Without reproducible data to test, this is a rough guess, but it should work:

storelist$Flag <- (storesales$Store_No %in% storelist$Store_NO &   
                   storesales$Calendar.Date == yesterday)+0L

Data Example

I test the structure of the above solution using the built-in mtcars dataset, I will create a flag column that checks if the cylinder variable is 4 or 6, and the horsepower variable is 110 or 175:

mtcars$Flag <- (mtcars$cyl %in% c(4,6) & mtcars$hp %in% c(110, 175))+0L
mtcars
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb Flag
# Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4    1
# Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4    1
# Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1    0
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1    1
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2    0
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1    0

Upvotes: 0

statsRhian
statsRhian

Reputation: 55

From the help page for Logical Operators, accessible by ?"&&":

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.

For example compare the difference between

> c(1,0,1) & c(1,1,0)
[1]  TRUE FALSE FALSE
> c(1,0,1) && c(1,1,0)
[1] TRUE

Upvotes: 1

Related Questions