or this variation if you don't want the first column to be named init:

 Reduce(cbind2, vec, 1:5)

On Tue, Jul 3, 2018 at 10:46 AM, Gabor Grothendieck
<ggrothendi...@gmail.com> wrote:
> Try Reduce:
>
>   Reduce(cbind, vec, 1:5)
>
> On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP)
> <wolfgang.viechtba...@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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to