Re: [R] ? extended rep()

2008-10-20 Thread Ted Harding
On 20-Oct-08 21:17:22, Gabor Grothendieck wrote:
> Try this:
> with(data.frame(x = 0:1, times = 3:6), rep(x, times))
> 
> or even shorter:
> do.call(rep, data.frame(x = 0:1, times = 3:6))

That is sneaky!

  data.frame(x = 0:1, times = 3:6)
#   x times
# 1 0 3
# 2 1 4
# 3 0 5
# 4 1 6

(Which is why it won't work with list(x=0:1,times=3:6))
Ted.

> On Mon, Oct 20, 2008 at 4:57 PM, Ted Harding
> <[EMAIL PROTECTED]> wrote:
>> Hi Folks,
>> I'm wondering if there's a compact way to achieve the
>> following. The "dream" is that, by analogy with
>>
>>  rep(c(0,1),times=c(3,4))
>> # [1] 0 0 0 1 1 1 1
>>
>> one could write
>>
>>  rep(c(0,1),times=c(3,4,5,6))
>>
>> which would produce
>>
>> # [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
>>
>> in effect "recycling" x through 'times'.
>>
>> The objective is to produce a vector of alternating runs of
>> 0s and 1s, with the lengths of the runs supplied as a vector.
>> Indeed, more generally, something like
>>
>>  rep(c(0,1,2), times=c(1,2,3,2,3,4))
>> # [1] 0 1 1 2 2 2 0 0 1 1 1 2 2 2 2
>>
>> Suggestions appreciated! With thanks,
>> Ted.
>>
>> 
>> E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 20-Oct-08   Time: 21:57:15
>> -- 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.
>>
> 
> __
> 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.


E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Oct-08   Time: 23:02:00
-- 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] ? extended rep()

2008-10-20 Thread Ted Harding
On 20-Oct-08 21:19:21, Stefan Evert wrote:
> On 20 Oct 2008, at 22:57, (Ted Harding) wrote:
>> I'm wondering if there's a compact way to achieve the
>> following. The "dream" is that one could write
>>
>>  rep(c(0,1),times=c(3,4,5,6))
>>
>> which would produce
>>
>> # [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
>>
>> in effect "recycling" x through 'times'.
> 
> rep2 <- function (x, times) rep(rep(x, length.out=length(times)),
> times)
> 
> rep2(c(0,1),times=c(3,4,5,6))
>   [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
> 
> Any prizes for shortest solution? ;-)
> 
> Best,
> Stefan

If ever we are both within reach of 'en øl', then yes.
But Gabor came up with a shorter one.

I tried to shorten Gabor's but failed.

However, all competitors are entitled to a consolation prize!
(And that includes me ... )
Ted.


E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Oct-08   Time: 22:59:16
-- 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] ? extended rep()

2008-10-20 Thread Gabor Grothendieck
Here is one other solution:

x <- 0:1
times <- 3:6
rep(x + 0*times, times)

This solution also works if the length of times is not a whole
number of lengths of x but in that case it does give a warning
which seems reasonable since that is the way recycling in
R works elsewhere too.

On Mon, Oct 20, 2008 at 5:17 PM, Gabor Grothendieck
<[EMAIL PROTECTED]> wrote:
> Try this:
>
> with(data.frame(x = 0:1, times = 3:6), rep(x, times))
>
> or even shorter:
>
> do.call(rep, data.frame(x = 0:1, times = 3:6))
>
>
> On Mon, Oct 20, 2008 at 4:57 PM, Ted Harding
> <[EMAIL PROTECTED]> wrote:
>> Hi Folks,
>> I'm wondering if there's a compact way to achieve the
>> following. The "dream" is that, by analogy with
>>
>>  rep(c(0,1),times=c(3,4))
>> # [1] 0 0 0 1 1 1 1
>>
>> one could write
>>
>>  rep(c(0,1),times=c(3,4,5,6))
>>
>> which would produce
>>
>> # [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
>>
>> in effect "recycling" x through 'times'.
>>
>> The objective is to produce a vector of alternating runs of
>> 0s and 1s, with the lengths of the runs supplied as a vector.
>> Indeed, more generally, something like
>>
>>  rep(c(0,1,2), times=c(1,2,3,2,3,4))
>> # [1] 0 1 1 2 2 2 0 0 1 1 1 2 2 2 2
>>
>> Suggestions appreciated! With thanks,
>> Ted.
>>
>> 
>> E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 20-Oct-08   Time: 21:57:15
>> -- 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.
>>
>

__
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] ? extended rep()

2008-10-20 Thread Stefan Evert


On 20 Oct 2008, at 22:57, (Ted Harding) wrote:


I'm wondering if there's a compact way to achieve the
following. The "dream" is that one could write

 rep(c(0,1),times=c(3,4,5,6))

which would produce

# [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1

in effect "recycling" x through 'times'.


rep2 <- function (x, times) rep(rep(x, length.out=length(times)), times)

rep2(c(0,1),times=c(3,4,5,6))
 [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1

Any prizes for shortest solution? ;-)

Best,
Stefan

__
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] ? extended rep()

2008-10-20 Thread Gabor Grothendieck
Try this:

with(data.frame(x = 0:1, times = 3:6), rep(x, times))

or even shorter:

do.call(rep, data.frame(x = 0:1, times = 3:6))


On Mon, Oct 20, 2008 at 4:57 PM, Ted Harding
<[EMAIL PROTECTED]> wrote:
> Hi Folks,
> I'm wondering if there's a compact way to achieve the
> following. The "dream" is that, by analogy with
>
>  rep(c(0,1),times=c(3,4))
> # [1] 0 0 0 1 1 1 1
>
> one could write
>
>  rep(c(0,1),times=c(3,4,5,6))
>
> which would produce
>
> # [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
>
> in effect "recycling" x through 'times'.
>
> The objective is to produce a vector of alternating runs of
> 0s and 1s, with the lengths of the runs supplied as a vector.
> Indeed, more generally, something like
>
>  rep(c(0,1,2), times=c(1,2,3,2,3,4))
> # [1] 0 1 1 2 2 2 0 0 1 1 1 2 2 2 2
>
> Suggestions appreciated! With thanks,
> Ted.
>
> 
> E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
> Fax-to-email: +44 (0)870 094 0861
> Date: 20-Oct-08   Time: 21:57:15
> -- 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.
>

__
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] ? extended rep()

2008-10-20 Thread jim holtman
will this do what you want:

> f.rep <- function(x, times){
+ # make sure the 'x' is long enough
+ x <- head(rep(x, length(times)), length(times))
+ rep(x, times)
+ }
>
> f.rep(c(0,1), c(3,4,5,6,7))
 [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
>


On Mon, Oct 20, 2008 at 4:57 PM, Ted Harding
<[EMAIL PROTECTED]> wrote:
> Hi Folks,
> I'm wondering if there's a compact way to achieve the
> following. The "dream" is that, by analogy with
>
>  rep(c(0,1),times=c(3,4))
> # [1] 0 0 0 1 1 1 1
>
> one could write
>
>  rep(c(0,1),times=c(3,4,5,6))
>
> which would produce
>
> # [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
>
> in effect "recycling" x through 'times'.
>
> The objective is to produce a vector of alternating runs of
> 0s and 1s, with the lengths of the runs supplied as a vector.
> Indeed, more generally, something like
>
>  rep(c(0,1,2), times=c(1,2,3,2,3,4))
> # [1] 0 1 1 2 2 2 0 0 1 1 1 2 2 2 2
>
> Suggestions appreciated! With thanks,
> Ted.
>
> 
> E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
> Fax-to-email: +44 (0)870 094 0861
> Date: 20-Oct-08   Time: 21:57:15
> -- 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] ? extended rep()

2008-10-20 Thread Ted Harding
Hi Folks,
I'm wondering if there's a compact way to achieve the
following. The "dream" is that, by analogy with

  rep(c(0,1),times=c(3,4))
# [1] 0 0 0 1 1 1 1

one could write

  rep(c(0,1),times=c(3,4,5,6))

which would produce

# [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1

in effect "recycling" x through 'times'.

The objective is to produce a vector of alternating runs of
0s and 1s, with the lengths of the runs supplied as a vector.
Indeed, more generally, something like

  rep(c(0,1,2), times=c(1,2,3,2,3,4))
# [1] 0 1 1 2 2 2 0 0 1 1 1 2 2 2 2

Suggestions appreciated! With thanks,
Ted.


E-Mail: (Ted Harding) <[EMAIL PROTECTED]>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Oct-08   Time: 21:57:15
-- 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.