Reputation: 53
I have the following data
A | B | C | D | E |
---|---|---|---|---|
1 | Date | Temp.10 | values | Criteria |
2 | 10/08/2020 | 1 | 0 | |
3 | 11/08/2020 | 1 | 0 | |
4 | 12/08/2020 | 1 | 0 | 0 |
5 | 13/08/2020 | 1 | 3 | 0 |
6 | 14/08/2020 | 1 | 13 | 0 |
7 | 15/08/2020 | 1 | 15 | 0 |
8 | 16/08/2020 | 1 | 7 | 1 |
9 | 17/08/2020 | 1 | 6 | 1 |
10 | 18/08/2020 | 1 | 0 | 1 |
11 | 19/08/2020 | 1 | 25 | 0 |
To meet the criteria (=1) the two previous days must have values higher than 5. In Excel I would just place my self in cell E4 and type:
=IF(AND(D3>5,D2>5),1,0)
And then pull down the formula.
I am trying to write this in R, but I am not succeeding!
Help much appreciated thank you :)
Upvotes: 0
Views: 69
Reputation: 3237
R syntax is quite different from Excel. I'll show you:
Excel: AND(this, that)
R: this & that
Excel: IF(condition, expression, otherwise)
R:
if(condition){
expression
} else {
otherwise
}
alternatively: ifelse(condition, expression, otherwise)
There's no need for, or possibility of, clicking-and-dragging in R. Lots of functions, including &
, can deal with vectorised input. (But note that if
does not.)
To index a 'previous' value, you'll need to use the lag()
function.
The equivalent of =IF(AND(D3>5,D2>5),1,0)
is just (lag(D,1) > 5) & (lag(D,2) > 5))
, where D is a numeric vector.
If your data is a matrix or data.table, you can get D with df[,4]
where df is the name of your talbe and, as in your example, D is the fourth column. Alternatively if your columns are named, you can use df[,"D"]
. So the code would be something like:
(lag(df[,"D"], 1) > 5) & (lag(df[,"D"], 2) > 5)
The above will return a logical
vector with TRUE
/FALSE
values. If you want 1's and 0's, put the whole thing inside as.numeric()
.
Upvotes: 2
Reputation: 145775
With dplyr
:
library(dplyr)
your_data %>%
mutate(
Criteria = ifelse(lag(values, 1) > 5 & lag(values, 2) > 5, 1, 0)
)
Upvotes: 3