1 year ago
#379168
TRS6
How to count the number of times a value changes, grouped by two variables in R
I have a dataframe similar to the constructed one below, but much larger.
id <- rep(c("a", "b", "c", "d"), each = 3)
date <- seq(as.Date("2019-01-27"), as.Date("2019-02-7"), by="days")
mode <- c(1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 2)
month <- c("Jan", "Jan", "Jan", "Jan", "Jan", "Feb", "Feb", "Feb", "Feb", "Feb", "Feb", "Feb")
data <- data.frame(id, date, mode, month)
data <- data %>% arrange(id, date)
I would like to count the amount of times the value in "mode" column changes, but grouped by id and month. I know I can count the number of times a variable switches with no group using this line below:
data$mode.switches <- cumsum(c(0,as.numeric(diff(data$mode))!=0))
But I need to incorporate grouping and cannot figure that aspect out. I tried utilizing dplyr
:
data <- data %>%
group_by(id, month) %>%
mutate(mode.switches = cumsum(c(0,as.numeric(diff(data$mode))!=0))) %>%
ungroup()
But that produces an error "mode.switches must be size 3 or 1, not 12." I also know I could separate the dataframe into a list of unique combinations of id and month and apply the function over the whole list, but my real dataframe would produce a list with ~3,000 elements, so I would prefer to avoid that. How can I perform this function over groups determined by two variables in a concise way? I would appreciate any help on this query!
r
grouping
cumsum
0 Answers
Your Answer