1-Dimensional Matrix Is Changed to a Vector in R

1-dimensional Matrix is changed to a vector in R

This is an R FAQ. You need to do a[3,,drop = FALSE].

Convert a matrix to a 1 dimensional array

Either read it in with 'scan', or just do as.vector() on the matrix. You might want to transpose the matrix first if you want it by rows or columns.

> m=matrix(1:12,3,4)
> m
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> as.vector(m)
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> as.vector(t(m))
[1] 1 4 7 10 2 5 8 11 3 6 9 12

How to turn a vector into a matrix in R?

Just use matrix:

matrix(vec,nrow = 7,ncol = 7)

One advantage of using matrix rather than simply altering the dimension attribute as Gavin points out, is that you can specify whether the matrix is filled by row or column using the byrow argument in matrix.

Can single dimension named vectors be created in R?

If you want something with a column name and that will print in the column format then use a single column matrix or data.frame:

vector <- matrix( c(1,2,3,4), dimnames=list(NULL, "a") )

vector <- data.frame( a=c(1,2,3,4) )

There is a 1d object type but rather confusingly it requires that the assignment of a single dimension value to be its length. See:

  ?dim

dim(vector)=1L
Error in dim(vector) = 1L :
dims [product 1] do not match the length of object [4]

> dim(vector)=4L
> vector
[1] 1 2 3 4
> str(vector)
num [1:4(1d)] 1 2 3 4

Actually the dim function help page doesn't appear to document the requirement that the product of the dim-result will equal the length. My guess is that your homework assignment was intended to get you to read the dim help page and then discover (as I just did) that a one-d object is possible but a bit confusing.

As it turns out the distinction between row and column vectors is not enforced:

> vector %*% matrix(1:16,4)
[,1] [,2] [,3] [,4]
[1,] 30 70 110 150
> t(vector) %*% matrix(1:16,4)
[,1] [,2] [,3] [,4]
[1,] 30 70 110 150
> t(vector) %*% matrix(1:16,4) %*% vector
[,1]
[1,] 1100
> vector %*% matrix(1:16,4) %*% vector
[,1]
[1,] 1100

R - Converting a multidimensional array to vector and back to multidimensional array

1) Suppose we have array arr as shown in the first line. Then use c to convert to a vector vec and perform the transformation of adding 100 to each element. Then reshape it back with the same names giving arr2.

arr <- array(1:24, 2:4, dimnames = list(letters[1:2], LETTERS[1:3], month.abb[1:4]))
vec <- c(arr)
arr2 <- replace(arr, TRUE, vec + 100)

# check
all(arr2 - arr == 100)
## [1] TRUE

2) This would also work:

arr2a <- array(vec + 100, dim(arr), dimnames = dimnames(arr))

identical(arr2, arr2a)
## [1] TRUE

Vectorizing a matrix

I think it will be difficult to find a more compact method than:

c(m)
[1] 1 2 3 4 5 6 7 8 9

However, if you want to retain a matrix structure, then this reworking of the dim attribute would be be effective:

dim(m) <- c(dim(m)[1]*dim(m)[2], 1)
m
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9

There would be more compact methods of getting the product of the dimensions but the above method emphasizes that the dim attribute is a two element vector for matrices. Other ways of getting the "9" in that example include:

> prod(dim(m))
[1] 9
> length(m)
[1] 9

Increase one more dimension of the vector or matrix in R

If X is a vector, then you can add a dimension using dim() and length() functions

X <- 1:5
X
##[1] 1 2 3 4 5

dim(X) <- c(length(X), 1)
X
## [,1]
##[1,] 1
##[2,] 2
##[3,] 3
##[4,] 4
##[5,] 5

If X is a matrix or an array with more than 2 dimensions and you want to add axis to be the second dimension:

X <- matrix(1:6, ncol=2)
X
## [,1] [,2]
##[1,] 1 4
##[2,] 2 5
##[3,] 3 6

dim(X) <- c(dim(X)[1], 1, dim(X)[-1])
dim (X)
##[1] 3 1 2

X
#, , 1
#
# [,1]
#[1,] 1
#[2,] 2
#[3,] 3
#
#, , 2
#
# [,1]
#[1,] 4
#[2,] 5
#[3,] 6


Related Topics



Leave a reply



Submit