[R] Sampling rows from a list

Erin Hodgess hodgess at gator.dt.uh.edu
Fri Sep 19 08:06:04 CEST 2003


Dear R People:

Here is a function that someone asked for today.

This is getting the sample from different rows from a list.

Hope this helps!

Sincerely,
Erin
> oe1

function(n) {

        a1 <- n%%3

        n1 <- seq(from=2,to=(n-1+a1),by=3)

        n2 <- c(1:n)[-n1]

        if(a1 == 1)n2 <- c(n2,n2[length(n2)])

        if(a1 == 2)n2 <- c(n2,n1[length(n1)])

        n3 <- length(n2)

        y <- rep(NA,n3)

        i <- 1

        while(i <= n3) {

                a4 <- n2[i]:n2[(i+1)]

                if(length(a4)==1)a4 <- c(a4,a4)

                y[i:(i+1)] <- sample(a4,replace=T,2)

                i <- i+2

        }

        return(y)

}

>




More information about the R-help mailing list