s-news
[Top] [All Lists]

Re: split() and rep() question

To: Martyn Colins <martyncollins10@yahoo.com>
Subject: Re: split() and rep() question
From: Patrick Burns <pburns@pburns.seanet.com>
Date: Thu, 05 Oct 2006 17:49:00 +0100
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <20061005150525.48017.qmail@web90311.mail.mud.yahoo.com>
References: <20061005150525.48017.qmail@web90311.mail.mud.yahoo.com>
User-agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)
I think this gets you to where you want to be:

orig <- matrix(1:45, 15,3)
intermed <- do.call('rbind', split(orig, rep(1:3, each=5)))
final <- intermed[rep(1:3, each=5),]


Patrick Burns
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Martyn Colins wrote:

Dear all, I have a matrix similar to this: [,1] [,2] [,3]
[1,] 1 16 31
[2,] 2 17 32
[3,] 3 18 33
[4,] 4 19 34
[5,] 5 20 35
[6,] 6 21 36
[7,] 7 22 37
[8,] 8 23 38
[9,] 9 24 39
[10,] 10 25 40
[11,] 11 26 41
[12,] 12 27 42
[13,] 13 28 43
[14,] 14 29 44
[15,] 15 30 45
and I would like an efficient method to produce [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 1 2 3 4 5 16 17 18 19 20 31 32 33 34 35
[2,] 1 2 3 4 5 16 17 18 19 20 31 32 33 34 35
[3,] 1 2 3 4 5 16 17 18 19 20 31 32 33 34 35
[4,] 1 2 3 4 5 16 17 18 19 20 31 32 33 34 35
[5,] 1 2 3 4 5 16 17 18 19 20 31 32 33 34 35
[6,] 6 7 8 9 10 21 22 23 24 25 36 37 38 39 40
[7,] 6 7 8 9 10 21 22 23 24 25 36 37 38 39 40
[8,] 6 7 8 9 10 21 22 23 24 25 36 37 38 39 40
[9,] 6 7 8 9 10 21 22 23 24 25 36 37 38 39 40
[10,] 6 7 8 9 10 21 22 23 24 25 36 37 38 39 40
[11,] 11 12 13 14 15 26 27 28 29 40 41 42 43 44 45
[12,] 11 12 13 14 15 26 27 28 29 40 41 42 43 44 45
[13,] 11 12 13 14 15 26 27 28 29 40 41 42 43 44 45
[14,] 11 12 13 14 15 26 27 28 29 40 41 42 43 44 45
[15,] 11 12 13 14 15 26 27 28 29 40 41 42 43 44 45
Does anyone know how to do this? Been trying various ways to split the matrix using split() and rep() but can't seem to do it. Thanks in advance for your help. Best,
Martyn

------------------------------------------------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates starting at 1¢/min. <http://us.rd.yahoo.com/mail_us/taglines/postman7/*http://us.rd.yahoo.com/evt=39666/*http://messenger.yahoo.com>


<Prev in Thread] Current Thread [Next in Thread>