0
$\begingroup$

Suppose, $c(1, 2, 3, 4)$ and $c(2, 4, 5, 6)$ are two vectors. Then in R or RStudio,

How to create a $4\times 2$ matrix from these two vectors?

Also,

how to add another vector $c(8, 9, 10, 11)$ as a column to the previous matrix?

$\endgroup$

1 Answer 1

2
$\begingroup$
x <- c(1, 2, 3, 4)

y <- c(2, 4, 5, 6)

z <- matrix(c(x, y), nrow = length(x))

zz <- cbind(z, c(8, 9, 10, 11))

$\endgroup$
10
  • $\begingroup$ The first solution works but is not as instructive as a simple cbind(x, y). $\endgroup$
    – Michael M
    May 2, 2020 at 19:46
  • $\begingroup$ @Nick F, it says that "function lenght is not found". How to fix this issue? $\endgroup$
    – gete
    May 2, 2020 at 19:54
  • $\begingroup$ @MichaelM It says that "function lenght is not found". How to fix this issue? $\endgroup$
    – gete
    May 2, 2020 at 20:00
  • $\begingroup$ By switching the t and the h ;-) $\endgroup$
    – Michael M
    May 2, 2020 at 20:56
  • $\begingroup$ @MichaelM fair point on cbind for all parts being cleaner. $\endgroup$
    – Nick F
    May 2, 2020 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.