Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index R matrix without it reverting to vector

Tags:

r

vector

matrix

I declared a 1 by 6 matrix A by saying:

A <- matrix(1:6, nrow=1)

I then do dim(A) and as expected I get 1 by 6...but then I do A[,2:5] and I would expect that to be a matrix of dimension 1 by 4 with entries 2,3,4,5...but instead dim( A[,2:5] ) gives me NULL! it degraded into a vector or something. How can I avoid this?

I am ultimately trying to do something like:

A[,a:b] %*% X[a:b,a:b] %*% t(A[,a:b])

varying a and b so I can multiply only parts of the above matrices together..but this breaks when A decays into a vector...

Thanks

like image 729
Palace Chan Avatar asked Jan 25 '12 19:01

Palace Chan


People also ask

How are matrices indexed?

In logical indexing, you use a single, logical array for the matrix subscript. MATLAB extracts the matrix elements corresponding to the nonzero values of the logical array. The output is always in the form of a column vector. For example, A(A > 12) extracts all the elements of A that are greater than 12.

How many ways can you index a list in R?

There are 3 ways to index a vector, matrix, data frame, or list in R: Using explicit integer indices (or negative integers) Using a Boolean vector (often created on-the-fly) Using names.

How do you replicate a matrix in R?

The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).

Can you make a vector of matrices R?

A vector can be created by using c() function. Vectors in R are the same as the arrays in C language which are used to hold multiple data values of the same type. Vectors can also be used to create matrices. Matrices can be created with the help of Vectors by using pre-defined functions in R Programming Language.

How to convert a matrix to a one-dimensional vector in R?

However, it is also possible to convert a matrix to a one-dimensional vector by rows. The following R programming code explains how to do this in R by using the t and as.vector functions: The previous output is containing row No. 1 first, row No. 2 second, and so on…

How do you index a matrix with a single vector?

It is possible to index a matrix with a single vector. While indexing in such a way, it acts like a vector formed by stacking columns of the matrix one after another. The result is returned as a vector. Two logical vectors can be used to index a matrix. In such situation, rows and columns where the value is TRUE is returned.

What is a matrix in R?

You will learn to create and modify matrix, and access matrix elements. Matrix is a two dimensional data structure in R programming. Matrix is similar to vector but additionally contains the dimension attribute. All attributes of an object can be checked with the attributes () function (dimension can be checked directly with the dim () function).

How to find the index of an element in a vector?

There are three ways to find the index of an element in a vector. Example > x <- sample(1:10) > x [1] 8 10 9 6 2 1 4 7 5 3. Using which > which(x == 6) [1] 4


1 Answers

Use ,drop=FALSE as an additional (trailing) argument to involving ].

Example:

R> M <- matrix(1:4,2,2)
R> M[,2]                  ## looses matrix class
[1] 3 4
R> M[,2,drop=FALSE]       ## forced to a n x 1 matrix
     [,1]
[1,]    3
[2,]    4
R> 

This may well be the main FAQ, but for compatibility reasons the behaviour is unlikely to change.

like image 82
Dirk Eddelbuettel Avatar answered Sep 18 '22 14:09

Dirk Eddelbuettel