On 02/01/2011 05:16 PM, Phil Spector wrote:
> 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.
>

If you read the documentation carefully, you'll see it's not consistent, 
though not for the reason the original poster may have meant.  Following 
the documentation, 'each' should be a non-negative integer.  Given that 
the paragraph immediately above is explicit about the input being a 
vector, it's rather obvious to expect that vector is not a valid 
argument for 'each'.

Just as

     rep(1:2, each=-1)

is an error from invalid argument, so one might wish

     rep(1:2, each=c(1,2))

were too, instead of silently doing the arguably wrong thing.

The funny part that

     is.integer(2)
     # FALSE

tells you 2 is not a good value for 'each' is another story.

vQ



>                     - Phil Spector
>                      Statistical Computing Facility
>                      Department of Statistics
>                      UC Berkeley
>                      spec...@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@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.


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

Reply via email to