Skip to content
Prev 301770 / 398503 Next

R- Help (looping)

Hello,

Try the following.

# make up some data
dds <- 1e3
nc <- 10
xss <- data.frame(matrix(runif(nc*dds, min=-1, max=1), ncol=nc))
names(xss) <- paste0("xs", 1:10)

# two functions with descriptive names
getControlLimits <- function(x, L = 3){
     mu <- mean(x)
     sigma <- sd(x)
     c(lcl = mu - L*sigma, ucl = mu + L*sigma)
}

countOutOfControl <- function(x, L = 3){
     cl <- getControlLimits(x, L = L)
     sum( x < cl["lcl"] | x > cl["ucl"] )
}

sapply(xss, getControlLimits)    # To know why the next returns all zeros
sapply(xss, countOutOfControl)    # No values outside control limits


The data comes from an uniform in the interval (-1, 1) therefore the sd 
is sqrt(1/3) = 0.577. Times 3 gives values for lcl and ucl clearly below 
and above -1 and 1, respectively.  (I've tried rnorm and the counts were 
not zero.)
But this is just a data example to see if the code works, and it does.

Hope this helps,

Rui Barradas

Em 30-07-2012 22:04, Wellington Silva escreveu: