I am new to R. I have created an object a:
a <- c(2,4,6,8,10,12,14,16,18,20)
I have performed the following operation on the vector:
a[!c(10,0,8,6,0)]
and I get the output as 4 10 14 20
I do understand that !c(10,0,8,6,0) produces the output as FALSE TRUE FALSE FALSE TRUE
I don't understand how the final results comes out to be 4 10 14 20
Can someone help?
We obtain the results because the logical vector is recycled (as its length is only 5 compared to
length(a)which is 10) to meet the end of the 'a' vector i..eIf we use that vector
It is easier to understand if we just pass
TRUE, then the TRUE is recycled to return all the elements or the reverse withFALSEThe recycling is mentioned in the documentation of
?ExtractIn most of the languages, 0 is considered as FALSE and other values as TRUE. So, when we negate the 0 (FALSE) is converted to TRUE and all others to FALSE