[R] Behavior of seq with vector from
baptiste auguie
ba208 at exeter.ac.uk
Fri May 22 11:46:12 CEST 2009
Hi,
Perhaps you can try this,
> seq.weave <- function(froms, by, length, ... ){
> c(
> matrix(c(sapply(froms, seq, by=by, length = length/2, ...)),
> nrow=length(froms), byrow=T)
> )
> }
>
> seq.weave(c(2, 3), by=3, length=8)
> seq.weave(c(2, 3, 4), by=2, length=8)
HTH,
baptiste
On 21 May 2009, at 21:08, Rowe, Brian Lee Yung (Portfolio Analytics)
wrote:
> Hello,
>
> I want to use seq with multiple from values and am getting unexpected
> (to me) behavior. I'm wondering if this behavior is intentional or
> not.
>
>> seq(2, by=3, length.out=4)
> [1] 2 5 8 11
>
>> seq(3, by=3, length.out=4)
> [1] 3 6 9 12
>
> Now if I want the combined sequence, I thought I could pass in c(2,3),
> and I get:
>> seq(c(2,3), by=3, length.out=8)
> [1] 2 6 8 12 14 18 20 24
>
> However, the result is not what I expected (i.e. what I wanted):
> [1] 2 3 5 6 8 9 11 12
>
> It seems that this is a consequence of vector recycling during the
> summation in seq.default:
> if (missing(to)) from + (0L:(length.out - 1L)) * by
>
> To get the value I want, I am using the following code:
>> sort(as.vector(apply(array(c(2,3)), 1, seq, by=3,length.out=4)))
> [1] 2 3 5 6 8 9 11 12
>
> So two questions:
> 1. Is seq designed/intended to be used with a vector from argument,
> and
> is this the desired behavior?
> 2. If so, is there a cleaner way of implementing what I want?
>
> Thanks,
> Brian
>
>
>
>
> --------------------------------------------------------------------------
> This message w/attachments (message) may be privileged...{{dropped:26}}
More information about the R-help
mailing list