I have a slight program issue I cannot seem to figure out. I am wondering how i can in an elegant way count the number of consecutive numbers in a sequence starting from different values per group in r
for example, we have a data frame with names and numbers and would like to find minimize the data frame keeping only 1 entry per name and in the other the number of consecutive entries per name
names <- c(rep("bob",5), rep("henry",5), rep("maria",5))
goals <- c(1,2,3,5,4, 4,3,4,5,2, 1,2,4,6,5)
input.df <- data.frame(names, goals)
so starting from 1 the output data frame would be like the one below, where "bob" has a 3, since he had goals from 1 to 3 sequential entries in goals, henry has 0, cause he did not have a 1 or any ordered entries and maria has 2 because she had entries from 1 to 2
names <- c("bob", "henry", "maria")
runs <- c("3", "0", "2")
output.df.from.1 <- data.frame(names, goals)
and starting from 3, both bob and maria would have a 0 but henry would now have a 3 since he has 3, 4, 5.
names <- c("bob", "henry", "maria")
runs <- c("0", "3", "0")
output.df.from.3 <- data.frame(names, goals)
I am certain there must be a simple solution to this but I have not been able to find any, however I might be searching for the wrong things.
Does anyone have a suggestion?