I have a data frame with clicks per condition. I want to calculate the cummulative sum of these clicks per condition as the data comes in. I am currently using the ifelse() function to do this. However, for the "no" part of the test, I would want to repeat the value that was created in the previous "yes" part until there is the next "yes". Currently I am using NA's to create a placeholder instead.
How can I repeat the value that was created for the last "yes" when the test of the ifelse function is "no" until the next "yes"?
I've made a minimal example:
clicked <- round(runif(n = 20),0)
condition <- sample(c("Intervention", "Control"), size = 20, replace = T)
df <- data.frame(clicked, condition)
df %>% select(clicked, condition) %>% group_by(condition) %>%
mutate(successes.intervention = ifelse(condition == "Intervention", cumsum(clicked), NA),
N.intervention = ifelse(condition == "Intervention", 1:n(), NA),
successes.control = ifelse(condition == "Control", cumsum(clicked), NA),
N.control = ifelse(condition == "Control", 1:n(), NA)))
I want the output to look like this:
clicked condition successes.intervention N.intervention successes.control N.control
<dbl> <chr> <dbl> <int> <dbl> <int>
1 0 Control 0 0 0 1
2 1 Control 0 0 1 2
3 0 Control 0 0 1 3
4 1 Intervention 1 1 1 3
5 0 Control 1 1 1 4
6 0 Intervention 1 2 1 4
7 0 Intervention 1 3 1 4
8 0 Control 1 3 1 5
9 0 Intervention 1 4 1 5
10 1 Intervention 2 5 1 5
How about this?
Walk-through:
lapply(..)iterates over the string literals (determined dynamically) and produces alist; when converted to adata.frame, thenmutatewill add the columns dynamicallycumsum(..), we verify thatconditionis what we want to summarize, and then cumulatively sum up the number ofclicks (orNAif not the desired condition)acrosswill iterate over all selected columns and return the row number (within the group) minus 1; it optionally renames the columns per the.names"glue" string. For this, I chose the already-createdsuccesses.*columns, since they were always broken down into the variousconditionlevels.acrossto make sure the leading values are0;tidyr::fillto fill-down theNAvalues imposed by the condition logicData, starting with
set.seed(42)for reproducibility: