How to find the indices of an R list meeting multiple criteria

旧城冷巷雨未停 提交于 2019-12-10 17:19:33

问题


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


来源:https://stackoverflow.com/questions/23137276/how-to-find-the-indices-of-an-r-list-meeting-multiple-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!