select row based on the condition of number appare

2019-07-19 05:50发布

问题:

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.

回答1:

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.

# given vector v, return vector of FALSEs, except at the first 2
f <- function(v) replace(logical(length(v)), which(v == 2)[1], TRUE)
transform(data, GOAL=as.logical(ave(value, group, FUN=f)))
#    group value  GOAL
# 1      a     1 FALSE
# 2      a     1 FALSE
# 3      a     2  TRUE
# 4      a     2 FALSE
# 5      a     1 FALSE
# 6      b     1 FALSE
# 7      b     2  TRUE
# 8      b     1 FALSE
# 9      b     2 FALSE
# 10     b     3 FALSE

The call to as.logical is necessary if you want TRUE/FALSE, since ave always returns a numeric vector. Without as.logical, you get 0s and 1s.



回答2:

Or you can just do

valueFirst <- 2
data[data$value==valueFirst,][1,]

Then you have the row. If you want only GOAL as output:

data[data$value==valueFirst,][1,3]