[R] repeating values in an index two by two

2013-11-11 Thread Federico Calboli
Hi All,

I am trying to create an index that returns something like

1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8

and so on and so forth until a predetermined value (which is obviously even).  
I am trying very hard to avoid for loops or for loops front ends.

I'd be obliged if anybody could offer a suggestion.

BW

F


signature.asc
Description: Message signed with OpenPGP using GPGMail
__
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] repeating values in an index two by two

2013-11-11 Thread andrija djurovic
Hi. Here are two approaches:

c(mapply(function(x,y) rep(c(x,y), 2), (1:10)[c(T,F)], (1:10)[c(F,T)]))

c(tapply(1:10, rep(1:(10/2), each=2), rep, 2), recursive=T)

Andrija





On Mon, Nov 11, 2013 at 1:11 PM, Federico Calboli
f.calb...@imperial.ac.ukwrote:

 Hi All,

 I am trying to create an index that returns something like

 1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8

 and so on and so forth until a predetermined value (which is obviously
 even).  I am trying very hard to avoid for loops or for loops front ends.

 I'd be obliged if anybody could offer a suggestion.

 BW

 F

 __
 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.



[[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] repeating values in an index two by two

2013-11-11 Thread Federico Calboli
Hi,

first off, thanks for the suggestion.  I managed to solve it by doing:

IND = rep(c(T,T,F,F), 5)
X = rep(NA, 20)
X[IND] = 1:10
X[!IND] = 1:10

which avoids any function -- I think mapply, apply etc call a for loop 
internally, which I'd rather avoid.

BW

F



On 11 Nov 2013, at 12:35, andrija djurovic djandr...@gmail.com wrote:

 Hi. Here are two approaches:
 
 c(mapply(function(x,y) rep(c(x,y), 2), (1:10)[c(T,F)], (1:10)[c(F,T)]))
 
 c(tapply(1:10, rep(1:(10/2), each=2), rep, 2), recursive=T)
 
 Andrija
 
 
 
 
 
 On Mon, Nov 11, 2013 at 1:11 PM, Federico Calboli f.calb...@imperial.ac.uk 
 wrote:
 Hi All,
 
 I am trying to create an index that returns something like
 
 1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8
 
 and so on and so forth until a predetermined value (which is obviously even). 
  I am trying very hard to avoid for loops or for loops front ends.
 
 I'd be obliged if anybody could offer a suggestion.
 
 BW
 
 F
 
 __
 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.
 
 



signature.asc
Description: Message signed with OpenPGP using GPGMail
__
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] repeating values in an index two by two

2013-11-11 Thread Patrick Burns

 f1
function(x) {
one - matrix(1:x, nrow=2)
as.vector(rbind(one, one))
}
environment: 0x0daaf1c0
 f1(8)
 [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8

Pat


On 11/11/2013 12:11, Federico Calboli wrote:

Hi All,

I am trying to create an index that returns something like

1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8

and so on and so forth until a predetermined value (which is obviously even).  
I am trying very hard to avoid for loops or for loops front ends.

I'd be obliged if anybody could offer a suggestion.

BW

F



__
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.



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')

__
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] repeating values in an index two by two

2013-11-11 Thread Carl Witthoft
Here's a rather extreme solution:

 foo-rep(1:6,each=2)
Rgames foo
 [1] 1 1 2 2 3 3 4 4 5 5 6 6

Rgames foo[rep(c(1,3,2,4),3)+rep(c(0,4,8),each=4)]
 [1] 1 2 1 2 3 4 3 4 5 6 5 6

In the general case, then, it would be something like

foo- rep(1:N, each = 2)  # foo is of length(2*N)

foo[rep(c(1,3,2,4),2*N/4 + rep( seq(0, 3*N/4,by=4),each=4)]

Note that the refolding requires the sequence to have length a multiple of
4.




Patrick Burns wrote
 f1
 function(x) {
  one - matrix(1:x, nrow=2)
  as.vector(rbind(one, one))
 }
 environment: 0x0daaf1c0
   f1(8)
   [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
 
 Pat
 
 
 On 11/11/2013 12:11, Federico Calboli wrote:
 Hi All,

 I am trying to create an index that returns something like

 1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8

 and so on and so forth until a predetermined value (which is obviously
 even).  I am trying very hard to avoid for loops or for loops front ends.

 I'd be obliged if anybody could offer a suggestion.

 BW

 F



 __
 

 R-help@

  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.

 
 -- 
 Patrick Burns

 pburns@.seanet

 twitter: @burnsstat @portfolioprobe
 http://www.portfolioprobe.com/blog
 http://www.burns-stat.com
 (home of:
   'Impatient R'
   'The R Inferno'
   'Tao Te Programming')
 
 __

 R-help@

  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.





--
View this message in context: 
http://r.789695.n4.nabble.com/repeating-values-in-an-index-two-by-two-tp4680210p4680234.html
Sent from the R help mailing list archive at Nabble.com.

__
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] repeating values in an index two by two

2013-11-11 Thread Charles Determan Jr
Here is another solution that is a bit more flexible

tmp - seq(8)
# split into your desired groups
max.groups - 2
tmp.g - split(tmp, ceiling(seq_along(tmp)/max.groups))

# do repeats, unlist, numeric index
as.numeric(unlist(rep(tmp.g, each = 2)))

Hope this works for you,
Charles


On Mon, Nov 11, 2013 at 10:16 AM, Carl Witthoft c...@witthoft.com wrote:

 Here's a rather extreme solution:

  foo-rep(1:6,each=2)
 Rgames foo
  [1] 1 1 2 2 3 3 4 4 5 5 6 6

 Rgames foo[rep(c(1,3,2,4),3)+rep(c(0,4,8),each=4)]
  [1] 1 2 1 2 3 4 3 4 5 6 5 6

 In the general case, then, it would be something like

 foo- rep(1:N, each = 2)  # foo is of length(2*N)

 foo[rep(c(1,3,2,4),2*N/4 + rep( seq(0, 3*N/4,by=4),each=4)]

 Note that the refolding requires the sequence to have length a multiple of
 4.




 Patrick Burns wrote
  f1
  function(x) {
   one - matrix(1:x, nrow=2)
   as.vector(rbind(one, one))
  }
  environment: 0x0daaf1c0
f1(8)
[1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
 
  Pat
 
 
  On 11/11/2013 12:11, Federico Calboli wrote:
  Hi All,
 
  I am trying to create an index that returns something like
 
  1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8
 
  and so on and so forth until a predetermined value (which is obviously
  even).  I am trying very hard to avoid for loops or for loops front
 ends.
 
  I'd be obliged if anybody could offer a suggestion.
 
  BW
 
  F
 
 
 
  __
 

  R-help@

   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.
 
 
  --
  Patrick Burns

  pburns@.seanet

  twitter: @burnsstat @portfolioprobe
  http://www.portfolioprobe.com/blog
  http://www.burns-stat.com
  (home of:
'Impatient R'
'The R Inferno'
'Tao Te Programming')
 
  __

  R-help@

   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.





 --
 View this message in context:
 http://r.789695.n4.nabble.com/repeating-values-in-an-index-two-by-two-tp4680210p4680234.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.




-- 
Charles Determan
Integrated Biosciences PhD Candidate
University of Minnesota

[[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] repeating values in an index two by two

2013-11-11 Thread Iakub Henschen
 n-7
 rep(seq(1,n,2), each=4)+c(0,1,0,1)
 [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8

rep(), seq(), rbind(), apply() ... whatever: internally there will always
be iteration via some loop :-)

Ia.


On Mon, Nov 11, 2013 at 11:16 AM, Carl Witthoft c...@witthoft.com wrote:

 Here's a rather extreme solution:

  foo-rep(1:6,each=2)
 Rgames foo
  [1] 1 1 2 2 3 3 4 4 5 5 6 6

 Rgames foo[rep(c(1,3,2,4),3)+rep(c(0,4,8),each=4)]
  [1] 1 2 1 2 3 4 3 4 5 6 5 6

 In the general case, then, it would be something like

 foo- rep(1:N, each = 2)  # foo is of length(2*N)

 foo[rep(c(1,3,2,4),2*N/4 + rep( seq(0, 3*N/4,by=4),each=4)]

 Note that the refolding requires the sequence to have length a multiple of
 4.




 Patrick Burns wrote
  f1
  function(x) {
   one - matrix(1:x, nrow=2)
   as.vector(rbind(one, one))
  }
  environment: 0x0daaf1c0
f1(8)
[1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
 
  Pat
 
 
  On 11/11/2013 12:11, Federico Calboli wrote:
  Hi All,
 
  I am trying to create an index that returns something like
 
  1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8
 
  and so on and so forth until a predetermined value (which is obviously
  even).  I am trying very hard to avoid for loops or for loops front
 ends.
 
  I'd be obliged if anybody could offer a suggestion.
 
  BW
 
  F
 
 
 
  __
 

  R-help@

   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.
 
 
  --
  Patrick Burns

  pburns@.seanet

  twitter: @burnsstat @portfolioprobe
  http://www.portfolioprobe.com/blog
  http://www.burns-stat.com
  (home of:
'Impatient R'
'The R Inferno'
'Tao Te Programming')
 
  __

  R-help@

   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.





 --
 View this message in context:
 http://r.789695.n4.nabble.com/repeating-values-in-an-index-two-by-two-tp4680210p4680234.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.


[[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] repeating values in an index two by two

2013-11-11 Thread William Dunlap
Or you can use the integer divide and remainder operators:
n - 30
x - seq(0, len=n)
+ (x %% 2) + (x %/% 4)*2 + 1 # period 2 oscillator + jump by 2 every fourth
[1]  1  2  1  2  3  4  3  4  5  6  5  6  7  8  7
   [16]  8  9 10  9 10 11 12 11 12 13 14 13 14 15 16

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf
 Of Iakub Henschen
 Sent: Monday, November 11, 2013 8:42 AM
 To: r-help@r-project.org
 Subject: Re: [R] repeating values in an index two by two
 
  n-7
  rep(seq(1,n,2), each=4)+c(0,1,0,1)
  [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
 
 rep(), seq(), rbind(), apply() ... whatever: internally there will always
 be iteration via some loop :-)
 
 Ia.
 
 
 On Mon, Nov 11, 2013 at 11:16 AM, Carl Witthoft c...@witthoft.com wrote:
 
  Here's a rather extreme solution:
 
   foo-rep(1:6,each=2)
  Rgames foo
   [1] 1 1 2 2 3 3 4 4 5 5 6 6
 
  Rgames foo[rep(c(1,3,2,4),3)+rep(c(0,4,8),each=4)]
   [1] 1 2 1 2 3 4 3 4 5 6 5 6
 
  In the general case, then, it would be something like
 
  foo- rep(1:N, each = 2)  # foo is of length(2*N)
 
  foo[rep(c(1,3,2,4),2*N/4 + rep( seq(0, 3*N/4,by=4),each=4)]
 
  Note that the refolding requires the sequence to have length a multiple of
  4.
 
 
 
 
  Patrick Burns wrote
   f1
   function(x) {
one - matrix(1:x, nrow=2)
as.vector(rbind(one, one))
   }
   environment: 0x0daaf1c0
 f1(8)
 [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
  
   Pat
  
  
   On 11/11/2013 12:11, Federico Calboli wrote:
   Hi All,
  
   I am trying to create an index that returns something like
  
   1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8
  
   and so on and so forth until a predetermined value (which is obviously
   even).  I am trying very hard to avoid for loops or for loops front
  ends.
  
   I'd be obliged if anybody could offer a suggestion.
  
   BW
  
   F
  
  
  
   __
  
 
   R-help@
 
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.
  
  
   --
   Patrick Burns
 
   pburns@.seanet
 
   twitter: @burnsstat @portfolioprobe
   http://www.portfolioprobe.com/blog
   http://www.burns-stat.com
   (home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')
  
   __
 
   R-help@
 
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.
 
 
 
 
 
  --
  View this message in context:
  http://r.789695.n4.nabble.com/repeating-values-in-an-index-two-by-two-
 tp4680210p4680234.html
  Sent from the R help mailing list archive at Nabble.com.
 
  __
  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.
 
 
   [[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.

__
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.