I agree with Bert. It is not clear what is generating a need for this
sequence, so it
is difficult to see what aspects need to be adjustable. If this specific
sequence is the only one you need, then Bert's code looks elegant to
me.
One note: c is a base function in R. Functions in R are
Elegance is in the eye of the beholder.
But I would have thought that anything you do would be some variation of:
c(rep(1:3,e=2,time=4),
rep(4:5,e=4,time=3),
rep(6:9,e=3,time=2) )
## yielding
[1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5 4
4 4 4 5 5 5 5 4
[42] 4 4 4 5 5
Dear R users,
I'd like to create a sequence/vector, for example,
1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5 4 4 4
4 5 5 5 5 4 4 4 4 5 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 6 6 6 7 7 7 8 8 8 9
9 9
So I did like this below.
a - 4
b - 3
c - 2
grp - c( rep(1:b, each=c,
Hi Kathryn,
Well, there's always:
make_num_seq-function(x=list(c(from=1,to=1,by=1,each=1,times=1))) {
return(c(unlist(lapply(x,
function(x) return(rep(seq(x[1],x[2],by=x[3]),each=x[4],times=x[5]))
}
make_num_seq(list(c(1,3,1,2,4),c(4,5,1,4,3),c(6,9,1,3,2)))
Jim
On Sun, May 24, 2015 at
4 matches
Mail list logo