How to Know a Dimension of Matrix or Vector in R

Is there a dimension function that works for vectors, matrices and arrays in R

You could write something like this, which would be analogous to NROW and NCOL.

DIM <- function(x) if(is.null(dim(x))) length(x) else dim(x)

I wouldn't return a length-2 vector if something only has one dimension. And don't use ifelse for control flow.

In R, how to extract matrix dimension names (or labels) and list those names in another vector?

You may use colnames to get the column names of the matrix.

observeEvent(oe1(),{
sample.R <<- oe1()
cm1 <<- colnames(oe1())
})

You can check this vector in the console after running the app.

cm1
[1] "1" "2"

If you are using these values in the app you don't need to use <<-.

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

Check matrix size

You are on the right track... all might work..

> all(dim(m)==c(3,3))
[1] TRUE

Dimensions of matrix multiplication

You have 2 problems. One is that R eagerly converts single-column matrices to vectors, so K[1:2,1] is interpreted as a vector, not a matrix. We can fix this with the drop = FALSE argument to [.

There's also an order-of-operations problem, %*% has higher precedence than *, so we need parentheses to make the * happen first:

(K[1:2, 1, drop = F] * FF[1]) %*% K1[1, 1:2]
# [,1] [,2]
# [1,] 4e+07 2e+07
# [2,] 2e+07 1e+07

Unrelated to your problem, but it seems strange to make FF an array, seems like a length-100 vector would be simpler.

What does it mean to equate a matrix with a vector in R

A matrix is a vector with dimension attributes, hence the possibility to compare it to other vectors. For matrix operations, the matrix is treated as having values arranged by column. As we can see in the following

X = matrix(1:9, 3, 3)

X
# [,1] [,2] [,3]
# [1,] 1 4 7
# [2,] 2 5 8
# [3,] 3 6 9

as.vector(X)
# [1] 1 2 3 4 5 6 7 8 9

We can see the dim attributes thus:

attributes(X)
# $dim
# [1] 3 3

If we remove the dim attributes, we are left with just a vector

attr(X, 'dim') = NULL
X
[1] 1 2 3 4 5 6 7 8 9

Creating a vector of matrices of different dimension in R

Use a list

C <- list()
C[[1]] <- array(0,dim=c(2,2))
C[[2]] <- array(0,dim=c(3,3))
C[[1]][1,1] <- 5
C[[1]]
C[[2]]

Is there a way to select all elements of a dimension when matrix-indexing a multidimensional array in R?

Simpler reproducible example:

array3d <- array(1:27,dim=c(3,3,3))
x <- array3d[,1,1]

In the comments @user2957945 points out that setting the 'blank' elements of the index vector to TRUE will allow do.call('[',...) to select all of the elements from that dimension.

i <- list(TRUE, 1, 1);  do.call('[', c(list(array3d), i))

Previous (suboptimal) answer:

I don't know if there's a simpler/better way, but this works without using str2lang/eval/etc.:

i <- c(NA,1,1)  ## NA denotes "get all elements from this dimension"
getfun <- function(a,i) {
i <- as.list(i)
for (j in seq_along(i)) {
if (all(is.na(i[[j]]))) i[[j]] <- seq(dim(a)[j])
}
v <- as.matrix(do.call(expand.grid,i))
a[v]
}
getfun(array3d,i)

R: changing dimensions of matrix

I agree with @flodel's comment, but here is a way:

m2 <- unstack(m, beep~day)
nrow <- max(sapply(m2, length))
m2 <- sapply(m2, function(x) {
length(x) <- nrow
x
})

# 17 18
#[1,] 74.50 58.25
#[2,] 77.50 81.25
#[3,] 89.50 NA
#[4,] 75.25 NA


Related Topics



Leave a reply



Submit