[R] Modifying the embed-results

Gabor Grothendieck ggrothendieck at gmail.com
Sun Aug 27 14:40:55 CEST 2006


On 8/27/06, Atte Tenkanen <attenka at utu.fi> wrote:
> Thanks!
> I see, that do.call-function is used often in R-algorithms...  Looking over some extra do.call-examples seems useful. This tail-function is also new for me.
>
> Is there some reason to use seq(along = VECTOR) instead of 1:length(VECTOR)?

If length(VECTOR) is 0 then 1:length(VECTOR) gives c(1,0) but
seq(along = VECTOR) gives a zero length vector.

> Atte
>
> > You can replace the for with lapply like this:
> >
> > VECTOR <- c(0, 3, 6, 3, 11, 2, 11, 4, 3, 4, 7, 7, 6, 4, 8)
> > f <- function(i) unique(tail(VECTOR, length(VECTOR)-i+1))[1:5]
> > out <- do.call(rbind, lapply(seq(along = VECTOR), f))
> > na.omit(rbind(rep(NA, 5), out))
> >
> > Note that  a matrix with zero rows is returned in the case
> > that VECTOR has zero length and in the case that VECTOR
> > has fewer than 5 unique elements.
> >
> >
> > On 8/26/06, Atte Tenkanen <attenka at utu.fi> wrote:
> > > Again my example was't very clear: there were not enough same
> > numbers in the VECTOR.
> > > What I need is something like this:
> > >
> > > VECTOR<-c(0,3,6,3,11,2,11,4,3,4,7,7,6,4,8)
> > > MATRIX<-c()
> > > for(i in 1:length(VECTOR)){
> > >        v<-(unique(VECTOR[i:length(VECTOR)])[1:5])
> > >        MATRIX<-rbind(MATRIX,v)
> > > }
> > >
> > > MATRIX<-na.omit(MATRIX)
> > > MATRIX
> > >
> > > > data.frame(MATRIX, row.names=NULL)
> > >  X1 X2 X3 X4 X5
> > > 1       0  3  6 11  2
> > > 2       3  6 11  2  4
> > > 3       6  3 11  2  4
> > > 4       3 11  2  4  7
> > > 5      11  2  4  3  7
> > > 6       2 11  4  3  7
> > > 7      11  4  3  7  6
> > > 8       4  3  7  6  8
> > > 9       3  4  7  6  8
> > >
> > > So, there are no duplicates in rows.
> > > VECTOR is always scanned forward as long as the number of  items
> > (here 5) becomes full.
> > >
> > > Atte
> > >
> > > > Try:
> > > >
> > > > embed(VECTOR, 5)[,5:1]
> > > >
> > > > On 8/25/06, Atte Tenkanen <attenka at utu.fi> wrote:
> > > > > Hi,
> > > > >
> > > > > Here is a vector and the result from the embed-command:
> > > > >
> > > > > VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8)
> > > > >
> > > > > > embed(VECTOR, dimension=5)
> > > > >      [,1] [,2] [,3] [,4] [,5]
> > > > >  [1,]   11    3    6    3    0
> > > > >  [2,]    2   11    3    6    3
> > > > >  [3,]    4    2   11    3    6
> > > > >  [4,]    3    4    2   11    3
> > > > >  [5,]    7    3    4    2   11
> > > > >  [6,]    6    7    3    4    2
> > > > >  [7,]    4    6    7    3    4
> > > > >  [8,]    5    4    6    7    3
> > > > >  [9,]   10    5    4    6    7
> > > > > [10,]    2   10    5    4    6
> > > > > [11,]    3    2   10    5    4
> > > > > [12,]    5    3    2   10    5
> > > > > [13,]    8    5    3    2   10
> > > > >
> > > > > Is there a way to little modify the algorithm so that the result
> > > > looks> like this:
> > > > >
> > > > > [1]  0  3  6 11  2 <- beginning from the first number of the
> > VECTOR> > > [1]  3  6 11  2  4 <- beginning from the second number
> > of the
> > > > VECTOR etc
> > > > > [1]  6  3 11  2  4
> > > > > [1]  3 11  2  4  7
> > > > > [1] 11  2  4  3  7
> > > > > [1] 2 4 3 7 6
> > > > > [1] 4 3 7 6 5
> > > > > [1] 3 7 6 4 5
> > > > > [1]  7  6  4  5 10
> > > > > [1]  6  4  5 10  2
> > > > > [1]  4  5 10  2  3
> > > > > [1]  5 10  2  3  8
> > > > > [1] 10  2  3  5  8
> > > > >
> > > > > Every row consists of next five unique(!) member of the
> > VECTOR. I
> > > > made> this example result with a time consuming algorithm which
> > > > uses for-loops
> > > > > and whiles.
> > > > >
> > > > > How to do this better??
> > > > >
> > > > > Thanks in advance!
> > > > >
> > > > > Atte Tenkanen
> > > > > University of Turku
> > > > >
> > > > > ______________________________________________
> > > > > R-help at stat.math.ethz.ch 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.
> > > > >
> > > >
> > >
> >
>



More information about the R-help mailing list