How to find the indices of an R list meeting multi

2019-06-25 23:26发布

问题:

Suppose I have the following data frame in R:

set.seed(5)
PosActions <- c("Work","Pause","Clockin","Clockout","Lunch")
df <- data.frame(ID = c(rep(1,3),rep(2:3,each=4),rep(4,5)), 
                 ACTION = sample(PosActions,16,replace=T))

Which returns

   ID   ACTION
1   1    Pause
2   1 Clockout
3   1    Lunch
4   2    Pause
5   2     Work
6   2 Clockout
7   2  Clockin
8   3    Lunch
9   3    Lunch
10  3     Work
11  3    Pause
12  4  Clockin
13  4    Pause
14  4  Clockin
15  4    Pause
16  4    Pause

In this data frame, the rows corresponding to ID == 2 and ID == 3 (rows 4 up to 11) contain the string "Work" in the column ACTION. I am trying to find the indices of these rows. In this case:

 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
[14] FALSE FALSE FALSE

In other words, whenever a set of rows with the same ID number contains "Work" in the ACTION column, all row indices of this ID number have to be returned.

I hope someone can help me, thanks in advance.

回答1:

Your question isn't totally clear. It sounds like you're looking for the following:

> df$ID %in% df$ID[which(df$ACTION == "Work")]
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[11]  TRUE FALSE FALSE FALSE FALSE FALSE

Step by step:

## Which rows have "Work" in the "ACTION" column?
> which(df$ACTION == "Work")
[1]  5 10

## What's the corresponding "ID" value, so we can subset on that?
> df$ID[which(df$ACTION == "Work")]
[1] 2 3