Caught with logical indexing and the โ€œwhichโ€ in R

Are there circumstances in R where the use whichfor indexing cannot be confused with logical indexing in R? I seem to recall that I met these two a few months ago - something with a taste of R, supporting some internal notion of line number, and it didnโ€™t go very well with using โ€œwhichโ€ after I used logical indexing in another location. discard multiple lines.

Is this a well-known phenomenon, or did I dream of all this?

+5
source share
2 answers

Remember that NA and other similar entries can be confusing. Following the @ mdsumner example:

> x <- c(1:10,NA,NaN,Inf)
> x > 5
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE    NA    NA
[13]  TRUE
> x[x > 5]
[1]   6   7   8   9  10  NA  NA Inf
> x[which(x > 5)]
[1]   6   7   8   9  10 Inf
+10

which / data.frame, ""

> 5 :

x <- 1:10
x > 5
[1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

10- , () 5:

(x > 5)  [1] 6 7 8 9 10

, , . -

x[x > 5]

x[which(x > 5)]

, "[" . , data.frame.

+6

All Articles