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

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Tue Jul 3 16:39:04 CEST 2018


If you put 1:5 vector to x you could do

cbind(x,t(replicate(length(x), vec)))

Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner's personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

> -----Original Message-----
> From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Viechtbauer,
> Wolfgang (SP)
> Sent: Tuesday, July 3, 2018 3:29 PM
> To: r-help using r-project.org
> Subject: [R] Combine by columns a vector with another vector that is constant
> across rows
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list