[R] repeating values in an index two by two

Federico Calboli f.calboli at imperial.ac.uk
Mon Nov 11 14:01:03 CET 2013


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 <djandrija at 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.calboli at 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 at 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.
> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 881 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131111/094d9112/attachment.bin>


More information about the R-help mailing list