My (sample) data look as follows:
mydata <- structure(list(x1 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), x2 = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 4L, 4L), x3 = c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 4L, 5L, 2L,
1L, 5L, 6L, 6L), week = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 30L, 50L,
22L, 52L, 36L, 25L, 26L), newar1 = c(0L, 0L, 2L, 0L, 0L, 2L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), newvar2 = c(0L, 2L, 0L, 0L,
2L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L)), .Names = c("x1", "x2",
"x3", "week", "newar1", "newvar2"), class = "data.frame", row.names = c(NA,
-14L))
x1 x2 x3 week
0 1 1 0
0 2 3 0
0 3 5 0
0 1 1 0
0 2 3 0
0 3 5 0
1 1 1 1
1 2 4 30
1 3 5 50
1 1 2 22
1 2 1 52
1 3 5 36
1 4 6 25
1 4 6 26
I would like to create 1 new variable newvar1
:
if x1 = 0 => I would like to count the number of times in the entire dataset where x1 equals 1 (only other rows, excluding the own observation), but only count rows with the same combination of x2 and x3 and rows where the week number is larger than 24.
if x1 = 1 => I would like to count the number of times in the entire dataset where x1 equals 1, but only count rows with the same combination of x2 and x3 and rows where the week number minus 25 is larger than zero ((week-25)>0)).
By "sum" I mean the number of times x1 equals 1 if the conditions hold.
By "if" I mean I only want to sum x1 when the conditions following the if hold. Basically my question is: how can I only sum certain values based on conditions?
My data should look like:
x1 x2 x3 week newvar1
0 1 1 0 0
0 2 3 0 0
0 3 5 0 2
0 1 1 0 0
0 2 3 0 0
0 3 5 0 2
1 1 1 1 0
1 2 4 30 0
1 3 5 50 1
1 1 2 22 0
1 2 1 52 0
1 3 5 36 0
1 4 6 25 0
1 4 6 26 1
Currently I have the following code, but this does not take into account the constraints for x2=x3
and for week. Any suggestions how to do this?
mydata[,newvar1:=sum(x1), by=list(x2,x3)]