The results of:
BB= RB[RB$Rep, %in% c(“1”,”3”)]
and
Bb=subset(RB,Rep ==c(“1”,”3”) )
are different.
Please tell me what the problem is?
The results of:
BB= RB[RB$Rep, %in% c(“1”,”3”)]
and
Bb=subset(RB,Rep ==c(“1”,”3”) )
are different.
Please tell me what the problem is?
When you use ==
the comparison is done in a sequential order.
Consider this example :
df <- data.frame(a = 1:6, b = c(1:3, 3:1))
df
# a b
#1 1 1
#2 2 2
#3 3 3
#4 4 3
#5 5 2
#6 6 1
When you use :
subset(df, b == c(1, 3))
# a b
#1 1 1
#4 4 3
1st value of b
is compared with 1, 2nd with 3. Now as you have vector of shorter length, the values are recycled meaning 3rd value is again compared to 1, 4th value with 3 and so on until end of the dataframe. Hence, you get row 1 and 4 as output here.
When you use %in%
it checks for either 1 or 3 is present in b
. So it selects all the rows where value 1 or 3 is present in b
.
subset(df, b %in% c(1, 3))
# a b
#1 1 1
#3 3 3
#4 4 3
#6 6 1