[R] one row matrix
Sarah Goslee
sarah.goslee at gmail.com
Fri Nov 3 23:22:44 CET 2006
On 11/3/06, Waverley <waverley.paloalto at gmail.com> wrote:
> Hi,
> For example:
>
> > a = matrix (12, 3, 4)
> > b = a[1,]
> > b
> [1] 12 12 12 12
>
It doesn't have anything to do with one-row matrices, at least in your example,
since that isn't what you made. Instead, you created a matrix with 3 rows,
4 columns, and all values equal to 12.
> a <- matrix(12, 3, 4)
> a
[,1] [,2] [,3] [,4]
[1,] 12 12 12 12
[2,] 12 12 12 12
[3,] 12 12 12 12
> a[1,]
[1] 12 12 12 12
You probably intended
> a <- matrix(c(12, 3, 4), nrow=1)
> a
[,1] [,2] [,3]
[1,] 12 3 4
But to solve what I think you were actually asking, even though it
wasn't reflected in your example... By default, R drops unused
dimensions when subsetting. You can override this behavior
> b <- a[1, ,drop=FALSE]
> b
[,1] [,2] [,3]
[1,] 12 3 4
See:
?subset
help('[')
?matrix
Sarah
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list