user46226
user46226

Reputation: 101

Why won't the which function return every element I request?

I have the following list in R, and I'm trying to extract all values from A$a that are greater than 3 using which(). I've tried the following:

A = list(a = c(2:5), b = c(3:5), c = c(4:6))
which(A$a > 3)

For some reason, it returns a vector of 3 and 4, and it excludes 5. How can I make which() return all values in A$a that are greater than 3?

Upvotes: 0

Views: 62

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57696

3 and 4 means the 3rd and 4th elements of A$a are greater than 3. The 3rd and 4th elements of A$a are 4 and 5.

If you want the elements themselves:

A$a[which(A$a > 3)]

Or just

A$a[A$a > 3]

Upvotes: 5

Related Questions