Phaml Phaml - 1 month ago 4
R Question

%in% ('in' operator) and || ('or' operator) used together to separate data sets mixed together

I want to separate the customers entering store by entering periods: who enter after t1/t2/t3 (different time). The following data are the all customers until the end of the time frame.

t1<-c(1:10) # at end of period t1,
t2<-c(1:7,12:15) #at the end of t2, id 1:7 from t1 stayed in the store and the rest of peopel from t1 left. but new customers with id 12:15 entered
t3<-c(3:9,12:14,20:25) # at end of t3, some in t1 stay and some in t2 stay and new people came.

enter1<-t1
enter2<-t2[!(t2 %in% t1)]
enter3<-t3[!(t3%in%t1||t3%in% t2)]


The enter3 code only give the result of

enter3[!(t3%in%t1)

Answer

You can also use setdiff to avoid the somewhat awkward x[!x %in% y] construction:

enter3 <- t3[!((t3 %in%t1) | (t3%in% t2))] # using akrun's recommendation  
e2 = setdiff(t2, t1)
e3 = setdiff(t3, union(t1, t2))
identical(enter2, e2)
# [1] TRUE
identical(enter3, e3)
# [1] TRUE
Comments