Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? In R, is there any function to do that?
views:
113answers:
3
+6
A:
> a <- c(1,2, 3.2, 4, 5)
> x <- 3
> a[a > x]
[1] 3.2 4.0 5.0
> min(a[a > x])
[1] 3.2
rcs
2010-08-29 07:31:06
On a sidenote: It's really worth looking a bit deeper into the use of indices in R. A good place to start is the help page (type ?"[" in the R console). Some hints can be found on the website of Quick-R as well : http://www.statmethods.net/management/subset.html
Joris Meys
2010-08-29 08:04:16
+1
A:
Or the longer one:
which(x < a)
## [1] 3 4 5
which(a > x)
## [1] 3 4 5
As you can see, it returns vector indices.
aL3xa
2010-08-29 15:09:38