Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the c command create a row vector or a column vector by default in R

Tags:

r

In R, when I use a command like this:

b <-c(7,10)
b

Does it create a row vector (1 row, 2 cols) or a column vector (1 col, 2 rows) by default?

I can't tell from the displayed output. I am R beginner (as is obvious :))

like image 686
Victor Avatar asked Jul 07 '15 19:07

Victor


People also ask

How do I turn a column vector into a row vector?

Transpose. You can convert a row vector into a column vector (and vice versa) using the transpose operator ' (an apostrophe).

Why vector is column matrix?

A column vector is an nx1 matrix because it always has 1 column and some number of rows. A row vector is a 1xn matrix, as it has 1 row and some number of columns.

Is a vector a row or a column in R?

You can treat a vector ( c() ) in R as a row or a column. It is a collection. it is made a column, such where the first index will address which column of the table is being referenced, in contradiction to what is orthodox in linear algebra.

How does R interpret a vector as a matrix?

If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix). So R will interpret a vector in whichever way makes the matrix product sensible.

How to treat a vector as a row or a column?

You can treat a vector ( c () ) in R as a row or a column. You can see this by It is a collection. By default tho when casting to a data frame it is made a column, such where the first index will address which column of the table is being referenced, in contradiction to what is orthodox in linear algebra.

How to convert data frame row to a vector?

Convert data frame row to a vector 1 Create dataframe 2 Select row to be converted 3 Pass it to the function 4 Display result More ...


1 Answers

Neither. A vector does not have a dimension attribute by default, it only has a length.

If you look at the documentation on matrix arithmetic, help("%*%"), you see that:

Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

So R will interpret a vector in whichever way makes the matrix product sensible.

Some examples to illustrate:

> b <- c(7,10)
> b
[1]  7 10
> dim(b) <- c(1,2)
> b
     [,1] [,2]
[1,]    7   10
> dim(b) <- c(2,1)
> b
     [,1]
[1,]    7
[2,]   10
> class(b)
[1] "matrix"
> dim(b) <- NULL
> b
[1]  7 10
> class(b)
[1] "numeric"

A matrix is just a vector with a dimension attribute. So adding an explicit dimension makes it a matrix, and R will do that in whichever way makes sense in context.

And an example of the behavior in the context of matrix multiplication:

> m <- matrix(1:2,1,2)
> m
     [,1] [,2]
[1,]    1    2
> m %*% b
     [,1]
[1,]   27
> m <- matrix(1:2,2,1)
> m %*% b
     [,1] [,2]
[1,]    7   10
[2,]   14   20
like image 152
joran Avatar answered Oct 01 '22 23:10

joran