[R] How to generate a particular sequence ?

2010-09-13 Thread Feng Li
Dear R,

I have a vector, say a = c(1,2,4,5,6,8). Can I generate a vector or array
(2-by-3-by-3) of this form c(1,2,1,2,1,2,4,5,4,5,4,5,6,8,6,8,6,8), in which
every two elements in a have been repeated twice?

I am to stupid today and could not figure this simple question out...  Many
many thanks!

Feng


-- 
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] How to generate a particular sequence ?

2010-09-13 Thread Jim Lemon

On 09/13/2010 07:19 PM, Feng Li wrote:

Dear R,

I have a vector, say a = c(1,2,4,5,6,8). Can I generate a vector or array
(2-by-3-by-3) of this form c(1,2,1,2,1,2,4,5,4,5,4,5,6,8,6,8,6,8), in which
every two elements in a have been repeated twice?

I am to stupid today and could not figure this simple question out...  Many
many thanks!


Hi Feng,
I would take a quick look at the help for rep and c, but I would 
first suggest that you count the number of times that the elements are 
to be repeated. While you have made the definition of the problem 
reasonably clear with your example, there are an awful lot of 
combinations of every two elements of a, whether you want to repeat 
them two or three times.


Jim

__
R-help@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.


Re: [R] How to generate a particular sequence ?

2010-09-13 Thread Ted Harding
On 13-Sep-10 09:19:21, Feng Li wrote:
 Dear R,
 I have a vector, say a = c(1,2,4,5,6,8). Can I generate a vector
 or array (2-by-3-by-3) of this form
 c(1,2,1,2,1,2,4,5,4,5,4,5,6,8,6,8,6,8)
 in which every two elements in a have been repeated twice?
 
 I am to stupid today and could not figure this simple question out... 
 Many many thanks!
 
 Feng

A possible solution (somewhat generalisable):

  a - c(1,2,4,5,6,8)
  Reps - 3

  pairs - matrix(a,nrow=2)
  as.vector(pairs[,rep(c(1,2,3),each=Reps)])
  # [1] 1 2 1 2 1 2 4 5 4 5 4 5 6 8 6 8 6 8

(By the way, you have 3 repetitions but wrote twice -- I assume
you meant thrice but the above generalises to 2 repetitions ... :)

Ted.


E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 13-Sep-10   Time: 10:42:46
-- XFMail --

__
R-help@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.


Re: [R] How to generate a particular sequence ?

2010-09-13 Thread Keith Jewell

Ted Harding ted.hard...@manchester.ac.uk wrote in message 
news:xfmail.100913104250.ted.hard...@manchester.ac.uk...
 On 13-Sep-10 09:19:21, Feng Li wrote:
 Dear R,
 I have a vector, say a = c(1,2,4,5,6,8). Can I generate a vector
 or array (2-by-3-by-3) of this form
 c(1,2,1,2,1,2,4,5,4,5,4,5,6,8,6,8,6,8)
 in which every two elements in a have been repeated twice?

 I am to stupid today and could not figure this simple question out...
 Many many thanks!

 Feng

 A possible solution (somewhat generalisable):

  a - c(1,2,4,5,6,8)
  Reps - 3

  pairs - matrix(a,nrow=2)
  as.vector(pairs[,rep(c(1,2,3),each=Reps)])
  # [1] 1 2 1 2 1 2 4 5 4 5 4 5 6 8 6 8 6 8

 (By the way, you have 3 repetitions but wrote twice -- I assume
 you meant thrice but the above generalises to 2 repetitions ... :)

 Ted.
The pedant in me couldn't resist asking:
  If he'd said repeated once would you expect only one occurence of each 
pair?

Sorry. I'll get my coat

Keith J

__
R-help@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.


Re: [R] How to generate a particular sequence ?

2010-09-13 Thread Feng Li
Sorry. It was my typo. Should be three times as it in the example.


Feng

On Mon, Sep 13, 2010 at 11:32 AM, Jim Lemon j...@bitwrit.com.au wrote:

 On 09/13/2010 07:19 PM, Feng Li wrote:

 Dear R,

 I have a vector, say a = c(1,2,4,5,6,8). Can I generate a vector or array
 (2-by-3-by-3) of this form c(1,2,1,2,1,2,4,5,4,5,4,5,6,8,6,8,6,8), in
 which
 every two elements in a have been repeated twice?

 I am to stupid today and could not figure this simple question out...
  Many
 many thanks!

  Hi Feng,
 I would take a quick look at the help for rep and c, but I would first
 suggest that you count the number of times that the elements are to be
 repeated. While you have made the definition of the problem reasonably clear
 with your example, there are an awful lot of combinations of every two
 elements of a, whether you want to repeat them two or three times.

 Jim




-- 
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] How to generate a particular sequence ?

2010-09-13 Thread Feng Li
Thanks. That's what I want. Sorry for the typo:)

Feng

On Mon, Sep 13, 2010 at 11:42 AM, Ted Harding
ted.hard...@manchester.ac.ukwrote:

 On 13-Sep-10 09:19:21, Feng Li wrote:
  Dear R,
  I have a vector, say a = c(1,2,4,5,6,8). Can I generate a vector
  or array (2-by-3-by-3) of this form
  c(1,2,1,2,1,2,4,5,4,5,4,5,6,8,6,8,6,8)
  in which every two elements in a have been repeated twice?
 
  I am to stupid today and could not figure this simple question out...
  Many many thanks!
 
  Feng

 A possible solution (somewhat generalisable):

  a - c(1,2,4,5,6,8)
   Reps - 3

  pairs - matrix(a,nrow=2)
  as.vector(pairs[,rep(c(1,2,3),each=Reps)])
  # [1] 1 2 1 2 1 2 4 5 4 5 4 5 6 8 6 8 6 8

 (By the way, you have 3 repetitions but wrote twice -- I assume
 you meant thrice but the above generalises to 2 repetitions ... :)

 Ted.

 
 E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
 Fax-to-email: +44 (0)870 094 0861
 Date: 13-Sep-10   Time: 10:42:46
 -- XFMail --




-- 
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

[[alternative HTML version deleted]]

__
R-help@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.