Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code double transpose a vector - is this a noop?

I have some legacy R code that does:

b = t(a)
c = t(b)

What does this code do? Looks like a noop to me. a is a vector constructed by c(1:20).

Edit: bonus points on how to do this better.

like image 415
Reactormonk Avatar asked Jan 08 '23 03:01

Reactormonk


1 Answers

Have a look at the structure using str:

> str(a); str(b); str(c)
 int [1:20] 1 2 3 4 5 6 7 8 9 10 ...
 int [1, 1:20] 1 2 3 4 5 6 7 8 9 10 ...
 int [1:20, 1] 1 2 3 4 5 6 7 8 9 10 ...

The final transpose operation sends the vector a to a matrix with 20 rows and 1 column. Equivalent to:

c <- as.matrix(c(1:20))
like image 160
Alex Avatar answered Feb 02 '23 20:02

Alex