# [R] Combine by columns a vector with another vector that is constant across rows

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Jul 3 17:47:56 CEST 2018

```Sorry trying again...

fastWolfgang <- function( v, vec ) {
matrix( c( v, rep( vec, each = length( v ) ) )
, nrow = length( v ) )
}

On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
>Gabor's solution seems to optimize 'simpler'.
>
>More efficient is to learn that in R a vector is not a matrix, but a
>matrix is just an ornamented vector.
>
>fastWolfgang <- function( v, vec ) {
>  matrix( c( v, rep( vec, length( v ) ) )
>         , now = length( v ) )
>}
>
>On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)"
><wolfgang.viechtbauer using maastrichtuniversity.nl> wrote:
>>Hi All,
>>
>>I have one vector that I want to combine with another vector and that
>>other vector should be the same for every row in the combined matrix.
>>This obviously does not work:
>>
>>vec <- c(2,4,3)
>>cbind(1:5, vec)
>>
>>This does, but requires me to specify the correct value for 'n' in
>>replicate():
>>
>>cbind(1:5, t(replicate(5, vec)))
>>
>>Other ways that do not require this are:
>>
>>t(sapply(1:5, function(x) c(x, vec)))
>>do.call(rbind, lapply(1:5, function(x) c(x, vec)))
>>t(mapply(c, 1:5, MoreArgs=list(vec)))
>>
>>I wonder if there is a simpler / more efficient way of doing this.
>>
>>Best,
>>Wolfgang
>>
>>______________________________________________
>>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>https://stat.ethz.ch/mailman/listinfo/r-help