Count IDs of groups if one variable is equal to a group

I have a data frame in R as shown below:

  Group.ID status
1        1   open
2        1   open
3        2   open
4        2 closed
5        2 closed
6        3   open

I want to calculate the number of identifiers, provided that when all status is “open” for the same ID number. For example, the identifier of group 1 has two observations, and their status is “open”, so one for my account. Group 2 identifier is not because not all statuses are open for group 2 identifier.

I can read strings or group identifiers in conditions. However, I do not know how to apply the logic "all state equal to one value for the group."

DATA

df1 <-
structure(list(Group.ID = c(1, 1, 2, 2, 2, 3), status = structure(c(2L, 
2L, 2L, 1L, 1L, 2L), .Label = c("closed", "open"), class = "factor")), .Names = c("Group.ID", 
"status"), row.names = c(NA, -6L), class = "data.frame")
+4
source share
3 answers

, base R, aggregate, tapply. , Group.ID , .

agg <- aggregate(status ~ Group.ID, df1, function(x) as.integer(all(x == "open")))
sum(agg$status)
#[1] 2

sum(tapply(df1$status, df1$Group.ID, FUN = function(x) all(x == "open")))
#[1] 2
+1

a dplyr :

library(dplyr)
df1 %>% 
  group_by(Group.ID) %>% 
  filter(cumsum(status == "open") == 2) %>%
  nrow()
0

, , .

: , , , "" "" , , , . script ?

sum (tapply (df1 $status, df1 $Group.ID, FUN = function (x) all (x == "open" x == "start" )))

@Rui Barradas

0

Source: https://habr.com/ru/post/1690076/


All Articles