I am struggling with this task: I have this dataframe:
df <- structure(list(col1 = c("A", "A", "A", "B", "A", "A", "C", "A"
)), class = "data.frame", row.names = c(NA, -8L))
col1
1 A
2 A
3 A
4 B
5 A
6 A
7 C
8 A
I want to get the count of A in the first sequence only.
The expected answer is 3!
Update: expected not working Output:
df %>%
summarise(first_sequence_A = sum(col1=="A"))
# not working because counting all A's
# resluting in:
first_sequence_A
1 6
expected:
first_sequence_A
1 3
I prefer a solution with dplyr
I have tried cumsum, rle, lag... but I can't get it!