R/winsor.mean.R

winsor.mean <-
function (x, k = 1, na.rm=TRUE) {
        if (any(is.na <- is.na(x))) {
            if (na.rm) 
                x <- x[!is.na]
            else return(NA)
        }
        n <- length(x)
        if (!(k %in% (0:n))) 
            stop("'k' should be > 0 and less than half the number of non-missing observations.")
        else {
            x <- sort(x)
            x[1:k] <- x[k+1] # Here I solve the lower values
            x[(n-k+1):n] <- x[n-k] #Then I go over the higher ones
            return(mean(x))
        }
    }

Try the SciencePo package in your browser

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

SciencePo documentation built on May 2, 2019, 5:53 p.m.