[R] row selection

David Winsemius dwinsemius at comcast.net
Thu Oct 8 22:31:13 CEST 2009


On Oct 8, 2009, at 4:18 PM, David Winsemius wrote:

>
> On Oct 8, 2009, at 4:14 PM, Ashta wrote:
>
>> Hi all,
>> I have a matrix  named x with N by  C
>> I want to select every 5 th rrow from matrix x
>> I used the following code
>> n<- nrow(x)
>>> for(i in 1: n){
>> + b <- a[i+5,]
>>> b
>> }
>> Error: subscript out of bounds
>
> What did you expect when "i" in your loop counter became one greater  
> than the number of rows?

Perhaps (assuming that b has been created with same dimensions as a:
for (i in seq(1, nrow(a), by= 5) { b=a[i,] }


Which I suspect will not give you what you want, either, since that  
code would overwrite be each time through the loop, so maybe:


for (i in seq(1, nrow(a), by= 5) { b=rbind(b, a[i,] ) }


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list