I have a vector of unique elements that I want to replicate a variable number of times depending on the element (lengths all > 800). However I noticed that the resulting length was not the sum of the each argument. The following example demonstrates this. I am confused as to why this works: rep(1:4, c(2,1,2,1)) [1] 1 1 2 3 3 4 but this doesn't: rep(1:3, each=c(2,1,2)) [1] 1 1 2 2 3 3 This does not appear to be consistent with the description provided in ?rep.... Is this supposed to be happening? -- View this message in context: http://r.789695.n4.nabble.com/each-argument-in-rep-Bug-tp3253050p3253050.html Sent from the R help mailing list archive at Nabble.com.
In what way is the behavior non-consistent? Quoting from the help page for rep: ?times? A vector giving the number of times to repeat each element if of length ?length(x)?, or to repeat the whole vector if of length 1. ?each? non-negative integer. Each element of ?x? is repeated ?each? times. Treated as ?1? if ?NA? or invalid. So while times may be a vector, each is clearly limited to being an integer. I'll admit that it would be nice if it told you that it was only using the first element of a vector each argument, but the behaviour is perfectly consistent with the documentation. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 1 Feb 2011, T.D. Rudolph wrote:> > I have a vector of unique elements that I want to replicate a variable number > of times depending on the element (lengths all > 800). However I noticed > that the resulting length was not the sum of the each argument. The > following example demonstrates this. > > I am confused as to why this works: > > rep(1:4, c(2,1,2,1)) > [1] 1 1 2 3 3 4 > > but this doesn't: > > rep(1:3, each=c(2,1,2)) > [1] 1 1 2 2 3 3 > > This does not appear to be consistent with the description provided in > ?rep.... > Is this supposed to be happening? > -- > View this message in context: http://r.789695.n4.nabble.com/each-argument-in-rep-Bug-tp3253050p3253050.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Tena koe ?rep states that each is a non-negative integer. Note, integer not a vector. Thus, rep(1:3, each=c(2,1,2)) only uses the first element in each and is equivalent to rep(1:3, each=2). One could argue there should be a warning (as there is for some functions is similar situations), but I don't see that it is a bug or undocumented (although the documentation is subtle). HTH .... Peter Alspach.> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of T.D. Rudolph > Sent: Wednesday, 2 February 2011 11:02 a.m. > To: r-help at r-project.org > Subject: [R] "each" argument in rep (Bug?) > > > I have a vector of unique elements that I want to replicate a variable > number > of times depending on the element (lengths all > 800). However I > noticed > that the resulting length was not the sum of the each argument. The > following example demonstrates this. > > I am confused as to why this works: > > rep(1:4, c(2,1,2,1)) > [1] 1 1 2 3 3 4 > > but this doesn't: > > rep(1:3, each=c(2,1,2)) > [1] 1 1 2 2 3 3 > > This does not appear to be consistent with the description provided in > ?rep.... > Is this supposed to be happening? > -- > View this message in context: http://r.789695.n4.nabble.com/each- > argument-in-rep-Bug-tp3253050p3253050.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.