2012-10-08 25 views
1

可能重复:
1-dimensional Matrix is changed to a vector in R[R避免矩阵胁迫数字

我与R中matrix对象,并多次工作,它发生,我想选择的只有一列矩阵一个使用它作为一列的matrix!是的,我的意思是我不希望R将它自动强制转换为numeric类,因为在这种情况下含义是1列的矩阵。如何避免R一直在一般水平上进行这种愚蠢的转换。我不想混乱我的代码到处都是as.matrix

+0

这_must_是重复 – GSee

+1

听起来像R-地狱的读是为了:http://www.burns-stat.com/pages/Tutor/R_inferno.pdf你目前在Circle 8.1.44。 – Aaron

+0

@GSee:快速搜索出现:http://stackoverflow.com/q/9949202/892313,http://stackoverflow.com/q/12601692/892313,http://stackoverflow.com/q/7598674/ 892313。我想,第一个或第三个将是最接近的副本。 –

回答

2

使用drop=FALSE

> matrix(1:10, ncol=2) 
    [,1] [,2] 
[1,] 1 6 
[2,] 2 7 
[3,] 3 8 
[4,] 4 9 
[5,] 5 10 
> matrix(1:10, ncol=2)[, 2] 
[1] 6 7 8 9 10 
> matrix(1:10, ncol=2)[, 2, drop=FALSE] 
    [,1] 
[1,] 6 
[2,] 7 
[3,] 8 
[4,] 9 
[5,] 10