[R] expand.grid using a matrix and a vector as input
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Thu Mar 15 13:56:16 CET 2012
One possibility is to use something like the following:
a1 <- matrix(1:4, 2, 2)
a2 <- c(8, 9)
cbind(a1[rep(1:nrow(a1), length(a2)), ], rep(a2, each = nrow(a1)))
I hope it helps.
Best,
Dimitris
On 3/15/2012 1:33 PM, eugen pircalabelu wrote:
>
> Hello R-users,
>
> I have the following question, for which my search did not really return any usable result.
> If I have a matrix a1, and a vector a2 like below
>
> a1<-matrix(c(1:4),2,2)
> a2<-c(8,9)
>
> is there any function like the expand.grid (or some clever calling of the function) such that it outputs a matrix or dataframe where the entire a1 matrix is repeated for each value of a2 (the rbind and cbind solution is not something that i am after).
> The desired output is:
>
> out=
> 1 3 8
> 2 4 8
> 1 3 9
> 2 4 9
>
> Thank you in advance and have a great day ahead!
> Eugen
>
>
> [[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> 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.
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
More information about the R-help
mailing list