[R] basic matrix function

arun smartpink111 at yahoo.com
Mon Sep 23 06:40:22 CEST 2013


Hi,
Use `drop=FALSE`.
 b<- matrix(c(2,1,-1,-2),ncol=1)
 b[1:3,1]
#[1]  2  1 -1
 b[1:3,1,drop=FALSE]
#or
b[1:3,,drop=FALSE]
#     [,1]
#[1,]    2
#[2,]    1
#[3,]   -1


A.K.



hi all, 

i got a small question tonight. 
> matrix(b,4)[] 
     [,1] 
[1,]    2 
[2,]    1 
[3,]   -1 
[4,]   -2 
> dim(matrix(betan,4)) 
[1] 4 1 
As shown, b is a 4X1 matrix. 

> matrix(betan,4)[1:3,1] 
[1]  2  1 -1 

However, I think the result should be 
     [,1] 
[1,]    2 
[2,]    1 
[3,]   -1 

How could I get the result above? 
Many thanks,



More information about the R-help mailing list