Skip to content
Back to formatted view

Raw Message

Message-ID: <CAAmySGO5Jke-KkdThUvpn456o0n4aDUg2j3TX6eRWATuo=CDEw@mail.gmail.com>
Date: 2011-10-05T16:17:29Z
From: R. Michael Weylandt
Subject: Populate a matrix
In-Reply-To: <A0C20390AF37844A9E2C783C9C268D8C496566EEB4@CCD1XM1101.ccd1.root4.net>

matrix(rep(y, each=length(x)), nrow=length(y), byrow=TRUE)
or less explicitly
 matrix(y, nrow=length(y),ncol=length(x))
Michael

On Wed, Oct 5, 2011 at 12:11 PM,  <fernando.cabrera at nordea.com> wrote:
> Hi guys
>
> I have vectors x <- c(1,2,3,4) and y <- c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4 columns (length(x)), and each row is the corresponding y element repeated length(x) times.
>
> 4,4,4,4
> 3,3,3,3
> 9,9,9,9
>
> Thanks.
>
> Fernando ?lvarez
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>