Matrix Transpose using R t() function

Viewed 66

For following argument

x <- c(1,3,4,24,1,2,2,2,1,3,3,1,1,0,8) 
A <- matrix(x,nrow = 5)
n <- ncol(A)
t(A)
meancol <- colMeans(A)
meancol
t(t(meancol))

I am writing to ask why for t(A), I only need put t() to get what I want, but for meancol,I need to put t(t()) to get the results.

1 Answers

Notice that the output of meancol <- colMeans(A) isn't a matrix, then the function transpose interprets it as a vector.

From the Details of the function t() we have:

Details This is a generic function for which methods can be written. The description here applies to the default and "data.frame" methods.

A data frame is first coerced to a matrix: see as.matrix. When x is a vector, it is treated as a column, i.e., the result is a 1-row matrix.

The vector is read as a 1-column matrix, therefore the result is a 1-row matrix.

Related