Skip to content

Matrix in R with identical rows or columns

August 11, 2012

My solution is to make simple functions that combine the concepts of rep and rbind / cbind:

rbind.rep <- function(x, times) matrix(x, times, length(x), byrow = TRUE)
cbind.rep <- function(x, times) matrix(x, length(x), times, byrow = FALSE)

And these functions can be used like:

> set.seed(1)
> cbind.rep(runif(3), 2)
          [,1]      [,2]
[1,] 0.2655087 0.2655087
[2,] 0.3721239 0.3721239
[3,] 0.5728534 0.5728534
> rbind.rep(runif(3), 2)
          [,1]      [,2]      [,3]
[1,] 0.9082078 0.2016819 0.8983897
[2,] 0.9082078 0.2016819 0.8983897
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: