tags:

views:

109

answers:

3

I have a large dataset (202k points). I know that there are 8 values over 0.5. I want to subset on those rows.

How do I find/return a list the row numbers where the values are > 0.5?

probably pretty basic, sorry.

thx.

+3  A: 

If the dataset is a vector named x:

(1:length(x))[x > 0.5]

If the dataset is a data.frame or matrix named x and the variable of interest is in column j:

(1:nrow(x))[x[,j] > 0.5]

But if you just want to find the subset and don't really need the row numbers, use

subset(x, x > 0.5)

for a vector and

subset(x, x[,j] > 0.5)

for a matrix or data.frame.

Rob Hyndman
Thanks tons! that did the trick. I kinda ended up figuring it out myself before finding out that this comment was posted, but this is exactly what I did to.
gregclimbs
A: 

Here's some dummy data:

D<-matrix(c(0.6,0.1,0.1,0.2,0.1,0.1,0.23,0.1,0.8,0.2,0.2,0.2),nrow=3)

Which looks like:

> D
     [,1] [,2] [,3] [,4]
[1,]  0.6  0.2 0.23  0.2
[2,]  0.1  0.1 0.10  0.2
[3,]  0.1  0.1 0.80  0.2

And here's the logical row index,

index <- (rowSums(D>0.5))>=1

You can use it to extract the rows you want:

PeakRows <- D[index,]

Which looks like this:

> PeakRows
     [,1] [,2] [,3] [,4]
[1,]  0.6  0.2 0.23  0.2
[2,]  0.1  0.1 0.80  0.2
Jacob
+2  A: 

which(x > 0.5)

Jonathan Chang