Skip to content

which() in subset()

2 messages · Charles Stangor, Rui Barradas

#
Hello,

To know why, just evaluate the condition, with 't1$' before 'version_1':

which(as.character(t1$version_1) %in% a) != 0
[1] TRUE TRUE


It allways evaluates to TRUE, therefore, subset() returns all rows.

See if this isn't simpler than both of your forms.

v2 <- subset(t1, version_1 %in% a)
v2
   id version_1
1  1     100-1
2  2     100-2

The trick is to use %in% when doing multiple comparisons. With the 
vector with length equal to the number of observations on the left hand 
side.

Hope this helps,

Rui Barradas

Em 13-07-2012 12:12, Charles Stangor escreveu: