Skip to content
Back to formatted view

Raw Message

Message-ID: <20110107075905.GA31502@praha1.ff.cuni.cz>
Date: 2011-01-07T07:59:05Z
From: Petr Savicky
Subject: Creating a Matrix from a vector with some conditions
In-Reply-To: <1294349671070-3178219.post@n4.nabble.com>

On Thu, Jan 06, 2011 at 01:34:31PM -0800, ADias wrote:
> 
> Hi
> 
> Suppose we have an object with strings:
> 
> A<-c("a","b","c","d")
> 
> Now I do:
> 
> B<-matrix(A,4,4, byrow=F)
> 
> and I get
> 
> a a a a
> b b b b
> c c c c
> d d d d
> 
> But what I really want is:
> 
> a b c d
> b c d a
> c d a b
> d a b c
> 
> How can I do this?

Try the following

  A <- c("a","b","c","d")
  B <- matrix(A, 5, 4)[1:4, ]

  #     [,1] [,2] [,3] [,4]
  #[1,] "a"  "b"  "c"  "d" 
  #[2,] "b"  "c"  "d"  "a" 
  #[3,] "c"  "d"  "a"  "b" 
  #[4,] "d"  "a"  "b"  "c" 

Petr Savicky.