I have the following dataframe with 0, 1, and NAs for IDs A to E over a one year period:
dat <- data.frame(
id = c("A", "B", "C", "D", "E"),
jan = c(0, 0, NA, 1, 0),
feb = c(0, 1, 1, 0, 0),
mar = c(0, 0, 1, 0, 1),
apr = c(0, NA, 0, NA, 1),
may = c(0, NA, 0, 0, 0),
jun = c(0, 0, 0, 0, 0),
jul = c(0, 0, 0, 0, 1),
aug = c(NA, 0, 0, 1, 1),
sep = c(NA, 0, 0, 1, NA),
okt = c(NA, 0, 0, 0, NA),
nov = c(NA, 0, 0, 0, 1),
dez = c(NA, 0, 0, 0, 0)
)
> dat
id jan feb mar apr may jun jul aug sep okt nov dez
A 0 0 0 0 0 0 0 NA NA NA NA NA
B 0 1 0 NA NA 0 0 0 0 0 0 0
C NA 1 1 0 0 0 0 0 0 0 0 0
D 1 0 0 NA 0 0 0 1 1 0 0 0
E 0 0 1 1 0 0 1 1 NA NA 1 0
I would like to count the number of 1s for each ID over this one year period, but the following conditions need to be met:
- The first occurrence of a 1 is always counted as 1
- NAs should be treated as 0s
- A second occurrence of a 1 is only counted, if it is preceded by six or more 0s/NAs
In my example, the count would be:
> dat
id jan feb mar apr may jun jul aug sep okt nov dez count
1 A 0 0 0 0 0 0 0 NA NA NA NA NA => 0
2 B 0 1 0 NA NA 0 0 0 0 0 0 0 => 1
3 C NA 1 1 0 0 0 0 0 0 0 0 0 => 1
4 D 1 0 0 NA 0 0 0 1 1 0 0 0 => 2
5 E 0 0 1 1 0 0 1 1 NA NA 1 0 => 1
The function should be applied rowwise in the form of apply(dat[, -1], 1, my_fun) and return a vector containing the count (i.e. 0, 1, 1, 2, 1). Does anybody have an idea how to achieve this?