Skip to content
Prev 318881 / 398521 Next

How to combine conditional argument and logical argument in R to create subset of data...

Hi,
No problem.
V1<-rep(c(rep(111,5),rep(222,5),rep(333,5)),2)
?length(V1)
#[1] 30

?V2<- c(1:30) #should be the same length as V1
Tem1<- cbind(V1,V2)
Tem2<-Tem1[1:20,]

Tem1[!Tem1[,2]%in%Tem2[,2],]
?# ???? V1 V2
?#[1,] 222 21
?#[2,] 222 22
?#[3,] 222 23
?#[4,] 222 24
?#[5,] 222 25
?#[6,] 333 26
?#[7,] 333 27
?#[8,] 333 28
?#[9,] 333 29
#[10,] 333 30

#or
subset(Tem1,!V2%in% Tem2[,2])
#or
?Tem1[is.na(match(Tem1[,2],Tem2[,2])),]
?# ???? V1 V2
?#[1,] 222 21
?#[2,] 222 22
?#[3,] 222 23
?#[4,] 222 24
?#[5,] 222 25
?#[6,] 333 26
?#[7,] 333 27
?#[8,] 333 28
?#[9,] 333 29
#[10,] 333 30
A.K.