I would like to find rows in which defined number appear (for example 2) for first time?
For example:
group <- c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b")
value <- c(1, 1, 2, 2, 1, 1, 2, 1, 2, 3)
GOAL <- c("FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE")
data <- data.frame(group, value, GOAL)
data
In the column "GOAL" would be the result. Thank you for your help in advance.
Or you can just do
Then you have the row. If you want only GOAL as output:
This way assumes each
group
has at least one 2. Although your sample data is ordered by group, the approach used here doesn't depend on this.The call to
as.logical
is necessary if you wantTRUE
/FALSE
, sinceave
always returns a numeric vector. Withoutas.logical
, you get 0s and 1s.