R/sampcla.R

Defines functions sampcla

Documented in sampcla

sampcla <- function(x, y = NULL, m) {
    
    n <- length(x)

    lev <-  sort(unique(x))
    nlev <- length(lev)
    ni <- c(table(x))

    if(length(m) == 1) 
        m <- rep(m, nlev)
    else
        if(length(m) != nlev)
            stop("\n\n    Length of argument 'm' must be =1 
                 or =the number of classes in vector'x'. \n\n") 
    
    s <- list()
    for(i in seq_len(nlev)) {
        m[i] <- min(m[i], ni[i])
        zs <- which(x == lev[i])
        if(is.null(y)) {
            s[[i]] <- sample(zs, size = m[i], replace = FALSE)
            } 
        else {
            zy <- y[zs]
            ## "order(y) = 3 7 etc." means that the 1st lower value is the component 3 of the vector,
            ## the 2nd lower value is the component 7 of the vector,
            id <- order(zy)
            u <- round(seq(1, ni[i], length = m[i]))
            s[[i]] <- zs[id[u]]
            }
        }
    s <- unlist(s)
    
    list(train = seq_len(n)[-s], test = s, lev = lev, ni = ni)
    
    }

Try the rchemo package in your browser

Any scripts or data that you put into this service are public.

rchemo documentation built on Sept. 11, 2024, 8:05 p.m.