Hi, I'm Wellington from Brazil and I have the following issue: I've been working on a project a for a while, and I'm having trouble in using the loop (for) I need to read a column (c1), and for each value of this column, I need to check if it's within the control limits So, I was trying to do this: For (k in 1: c1) If (c1< lcl1 | c1 > ucl1) {here I need to store the values outside the limits) I have 5 columns, need to do the same process in each one of them. And later on I'm gonna concatenate these 5 vectors and calculate its mean for an ARL project. [[alternative HTML version deleted]]
Hello, Your code example doesn't make much sense, it needs decrypting. Let's see, for(k in 1:length(c1)){ if(c1[k] < lcl | c1[k] > ucl) {do something} } If this is it, then you can completely avoid the loop: i1 <- c1 < lcl | c1 > ucl # create an index vector out.of.control <- c1[ i1 ] # save the values When you say you have a column, is it a matrix column? data.frame? Give a data example with dput( head(myData, 20) ) # paste the output of this in a post Hope this helps, Rui Barradas Em 29-07-2012 21:59, Wellington G. Silva escreveu:> Hi, > > > > I'm Wellington from Brazil and I have the following issue: > > > > I've been working on a project a for a while, and I'm having trouble in > using the loop (for) > > > > I need to read a column (c1), and for each value of this column, I need to > check if it's within the control limits > > > > So, I was trying to do this: > > > > For (k in 1: c1) > > > > If (c1< lcl1 | c1 > ucl1) {here I need to store the values outside the > limits) > > > > I have 5 columns, need to do the same process in each one of them. > > > > And later on I'm gonna concatenate these 5 vectors and calculate its mean > for an ARL project. > > > > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.
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:> Ok Rui, > > I'll try to explain myself a little bit better. > > I have 10 columns, with 1000 rows each. (Each column represents a variable > of the process) > > these columns were simulated using runif, with values between 4 and 6. > > I need to calculate the control limits for each column (variable), and > verify if they are within the bounds accepted. > > For now, I'm reading these columns separately, I'm creating variables to > read only one of the each column, and then check if the values in this > column is within the limits or not. > > PS. All these columns originally come from a dataframe. something like > this: (dds = 1000) > > xs1<-runif(dds, min=-1, max=1) > xs2<-runif(dds, min=-1, max=1) > xs3<-runif(dds, min=-1, max=1) > xs4<-runif(dds, min=-1, max=1) > xs5<-runif(dds, min=-1, max=1) > ... > xs10<-runif(dds, min=-1,max=1) > xs<-data.frame(xs1,xs2,xs3,xs4,xs5,..xs10) > > _____________________________________________ > > > #Reading columns (first one) > c.n1 <- 'xs1' > c1 <- with(xss, get(c.n1)) > #Mean > mc1 <- mean (c1) > #Standard deviation > sdc1 <- sd(c1) > #Control Limits > L=3 > uclc1 = mc1 + L*sdc1 > lclc1 = mc1 - L*sdc1 > > > I need an "if" inside a loop to do the following: > > for (each column) > > if (value in c1 < lclc1 | value in c1 > uclc1) {Count how many values are > outside the bounds} > > And I need this process to be repeated to all columns. > > > > > 2012/7/30 Rui Barradas <ruipbarradas at sapo.pt> > >> Hello, >> >> Your code example doesn't make much sense, it needs decrypting. Let's see, >> >> for(k in 1:length(c1)){ >> if(c1[k] < lcl | c1[k] > ucl) {do something} >> } >> >> If this is it, then you can completely avoid the loop: >> >> i1 <- c1 < lcl | c1 > ucl # create an index vector >> out.of.control <- c1[ i1 ] # save the values >> >> When you say you have a column, is it a matrix column? data.frame? >> Give a data example with >> >> dput( head(myData, 20) ) # paste the output of this in a post >> >> Hope this helps, >> >> Rui Barradas >> >> Em 29-07-2012 21:59, Wellington G. Silva escreveu: >> >>> Hi, >>> >>> >>> I'm Wellington from Brazil and I have the following issue: >>> >>> >>> I've been working on a project a for a while, and I'm having trouble in >>> using the loop (for) >>> >>> >>> I need to read a column (c1), and for each value of this column, I need to >>> check if it's within the control limits >>> >>> >>> So, I was trying to do this: >>> >>> >>> For (k in 1: c1) >>> >>> >>> If (c1< lcl1 | c1 > ucl1) {here I need to store the values outside the >>> limits) >>> >>> >>> I have 5 columns, need to do the same process in each one of them. >>> >>> >>> And later on I'm gonna concatenate these 5 vectors and calculate its mean >>> for an ARL project. >>> >>> >>> >>> >>> >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________**________________ >>> R-help at r-project.org mailing list >>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >>> PLEASE do read the posting guide http://www.R-project.org/** >>> posting-guide.html <http://www.R-project.org/posting-guide.html> >>> and provide commented, minimal, self-contained, reproducible code. >>> >> >
Hello, Inline Em 31-07-2012 02:59, Wellington Silva escreveu:> Ok, > > This really helped. > > You've probably noticed, I'm a begginer using R. > > And when you said that you tried with rnorm and the counts were not zero, > where did you use the rnorm?Where the runif is, in its stead use matrix( rnorm(nc*dds), ...etc... ) But note that this is fake data, what is the distribution of your dataset? Or is the purpose of this simulations? If so, there must also be a distribution to replicate, no? Rui Barradas> > The point is, I need this count to be different from zero, so I can use the > ARL later on. > > I need how many values are out of control (for each column /variable), > store these values in a vector, calculate this vector's mean, and then > calculate the ARL. > > I've got to the same point as you did, but I'm stuck in the same problem. I > need this count do be different from zero. > > I'm still learning, haha, please be patient. > > Best Regards. > > 2012/7/30 Rui Barradas <ruipbarradas at sapo.pt> > >> 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: >> >>> Ok Rui, >>> >>> I'll try to explain myself a little bit better. >>> >>> I have 10 columns, with 1000 rows each. (Each column represents a variable >>> of the process) >>> >>> these columns were simulated using runif, with values between 4 and 6. >>> >>> I need to calculate the control limits for each column (variable), and >>> verify if they are within the bounds accepted. >>> >>> For now, I'm reading these columns separately, I'm creating variables to >>> read only one of the each column, and then check if the values in this >>> column is within the limits or not. >>> >>> PS. All these columns originally come from a dataframe. something like >>> this: (dds = 1000) >>> >>> xs1<-runif(dds, min=-1, max=1) >>> xs2<-runif(dds, min=-1, max=1) >>> xs3<-runif(dds, min=-1, max=1) >>> xs4<-runif(dds, min=-1, max=1) >>> xs5<-runif(dds, min=-1, max=1) >>> ... >>> xs10<-runif(dds, min=-1,max=1) >>> xs<-data.frame(xs1,xs2,xs3,**xs4,xs5,..xs10) >>> >>> ______________________________**_______________ >>> >>> >>> #Reading columns (first one) >>> c.n1 <- 'xs1' >>> c1 <- with(xss, get(c.n1)) >>> #Mean >>> mc1 <- mean (c1) >>> #Standard deviation >>> sdc1 <- sd(c1) >>> #Control Limits >>> L=3 >>> uclc1 = mc1 + L*sdc1 >>> lclc1 = mc1 - L*sdc1 >>> >>> >>> I need an "if" inside a loop to do the following: >>> >>> for (each column) >>> >>> if (value in c1 < lclc1 | value in c1 > uclc1) {Count how many values are >>> outside the bounds} >>> >>> And I need this process to be repeated to all columns. >>> >>> >>> >>> >>> 2012/7/30 Rui Barradas <ruipbarradas at sapo.pt> >>> >>> Hello, >>>> Your code example doesn't make much sense, it needs decrypting. Let's >>>> see, >>>> >>>> for(k in 1:length(c1)){ >>>> if(c1[k] < lcl | c1[k] > ucl) {do something} >>>> } >>>> >>>> If this is it, then you can completely avoid the loop: >>>> >>>> i1 <- c1 < lcl | c1 > ucl # create an index vector >>>> out.of.control <- c1[ i1 ] # save the values >>>> >>>> When you say you have a column, is it a matrix column? data.frame? >>>> Give a data example with >>>> >>>> dput( head(myData, 20) ) # paste the output of this in a post >>>> >>>> Hope this helps, >>>> >>>> Rui Barradas >>>> >>>> Em 29-07-2012 21:59, Wellington G. Silva escreveu: >>>> >>>> Hi, >>>>> >>>>> I'm Wellington from Brazil and I have the following issue: >>>>> >>>>> >>>>> I've been working on a project a for a while, and I'm having trouble in >>>>> using the loop (for) >>>>> >>>>> >>>>> I need to read a column (c1), and for each value of this column, I need >>>>> to >>>>> check if it's within the control limits >>>>> >>>>> >>>>> So, I was trying to do this: >>>>> >>>>> >>>>> For (k in 1: c1) >>>>> >>>>> >>>>> If (c1< lcl1 | c1 > ucl1) {here I need to store the values outside the >>>>> limits) >>>>> >>>>> >>>>> I have 5 columns, need to do the same process in each one of them. >>>>> >>>>> >>>>> And later on I'm gonna concatenate these 5 vectors and calculate its >>>>> mean >>>>> for an ARL project. >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> [[alternative HTML version deleted]] >>>>> >>>>> ______________________________****________________ >>>>> R-help at r-project.org mailing list >>>>> https://stat.ethz.ch/mailman/****listinfo/r-help<https://stat.ethz.ch/mailman/**listinfo/r-help> >>>>> <https://stat.**ethz.ch/mailman/listinfo/r-**help<https://stat.ethz.ch/mailman/listinfo/r-help> >>>>> PLEASE do read the posting guide http://www.R-project.org/** >>>>> posting-guide.html <http://www.R-project.org/**posting-guide.html<http://www.R-project.org/posting-guide.html> >>>>> and provide commented, minimal, self-contained, reproducible code. >>>>> >>>>> >
Hello, Glad it helped. E fico ? espera do relat?rio. Rui Barradas Em 31-07-2012 14:40, Wellington Silva escreveu:> Rui, > > This is a cientific initiation program. > > The idea is to develop a code which can simulate data and calculate the ARL > later. > > *So, a little bit later yesterday night, after sending the email, I've > figuered how to use the rnorm and then my problems were gone. > > Ok, we were working with normal distribution, that's why I needed to use > rnorm. These simulations have just one purpose: calculate ARL at the end of > the project. > > And, I want to thank you for the help, thank you so much, you've really > helped me. > > As soon as I'm done with the final report, at the "special thanks" page, > I'm gonna put your name Haha. Because, your help was enormous. > > Thanks again, I'll send you a copy of the final report as soon as I finish > it if you wish. > > 2012/7/31 Rui Barradas <ruipbarradas at sapo.pt> > >> Hello, >> >> Inline >> Em 31-07-2012 02:59, Wellington Silva escreveu: >> >> Ok, >>> This really helped. >>> >>> You've probably noticed, I'm a begginer using R. >>> >>> And when you said that you tried with rnorm and the counts were not zero, >>> where did you use the rnorm? >>> >> Where the runif is, in its stead use >> matrix( rnorm(nc*dds), ...etc... ) >> >> But note that this is fake data, what is the distribution of your dataset? >> Or is the purpose of this simulations? If so, there must also be a >> distribution to replicate, no? >> >> Rui Barradas >> >>> The point is, I need this count to be different from zero, so I can use >>> the >>> ARL later on. >>> >>> I need how many values are out of control (for each column /variable), >>> store these values in a vector, calculate this vector's mean, and then >>> calculate the ARL. >>> >>> I've got to the same point as you did, but I'm stuck in the same problem. >>> I >>> need this count do be different from zero. >>> >>> I'm still learning, haha, please be patient. >>> >>> Best Regards. >>> >>> 2012/7/30 Rui Barradas <ruipbarradas at sapo.pt> >>> >>> 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: >>>> >>>> Ok Rui, >>>>> I'll try to explain myself a little bit better. >>>>> >>>>> I have 10 columns, with 1000 rows each. (Each column represents a >>>>> variable >>>>> of the process) >>>>> >>>>> these columns were simulated using runif, with values between 4 and 6. >>>>> >>>>> I need to calculate the control limits for each column (variable), and >>>>> verify if they are within the bounds accepted. >>>>> >>>>> For now, I'm reading these columns separately, I'm creating variables to >>>>> read only one of the each column, and then check if the values in this >>>>> column is within the limits or not. >>>>> >>>>> PS. All these columns originally come from a dataframe. something like >>>>> this: (dds = 1000) >>>>> >>>>> xs1<-runif(dds, min=-1, max=1) >>>>> xs2<-runif(dds, min=-1, max=1) >>>>> xs3<-runif(dds, min=-1, max=1) >>>>> xs4<-runif(dds, min=-1, max=1) >>>>> xs5<-runif(dds, min=-1, max=1) >>>>> ... >>>>> xs10<-runif(dds, min=-1,max=1) >>>>> xs<-data.frame(xs1,xs2,xs3,****xs4,xs5,..xs10) >>>>> >>>>> ______________________________****_______________ >>>>> >>>>> >>>>> >>>>> #Reading columns (first one) >>>>> c.n1 <- 'xs1' >>>>> c1 <- with(xss, get(c.n1)) >>>>> #Mean >>>>> mc1 <- mean (c1) >>>>> #Standard deviation >>>>> sdc1 <- sd(c1) >>>>> #Control Limits >>>>> L=3 >>>>> uclc1 = mc1 + L*sdc1 >>>>> lclc1 = mc1 - L*sdc1 >>>>> >>>>> >>>>> I need an "if" inside a loop to do the following: >>>>> >>>>> for (each column) >>>>> >>>>> if (value in c1 < lclc1 | value in c1 > uclc1) {Count how many values >>>>> are >>>>> outside the bounds} >>>>> >>>>> And I need this process to be repeated to all columns. >>>>> >>>>> >>>>> >>>>> >>>>> 2012/7/30 Rui Barradas <ruipbarradas at sapo.pt> >>>>> >>>>> Hello, >>>>> >>>>>> Your code example doesn't make much sense, it needs decrypting. Let's >>>>>> see, >>>>>> >>>>>> for(k in 1:length(c1)){ >>>>>> if(c1[k] < lcl | c1[k] > ucl) {do something} >>>>>> } >>>>>> >>>>>> If this is it, then you can completely avoid the loop: >>>>>> >>>>>> i1 <- c1 < lcl | c1 > ucl # create an index vector >>>>>> out.of.control <- c1[ i1 ] # save the values >>>>>> >>>>>> When you say you have a column, is it a matrix column? data.frame? >>>>>> Give a data example with >>>>>> >>>>>> dput( head(myData, 20) ) # paste the output of this in a post >>>>>> >>>>>> Hope this helps, >>>>>> >>>>>> Rui Barradas >>>>>> >>>>>> Em 29-07-2012 21:59, Wellington G. Silva escreveu: >>>>>> >>>>>> Hi, >>>>>> >>>>>>> I'm Wellington from Brazil and I have the following issue: >>>>>>> >>>>>>> >>>>>>> I've been working on a project a for a while, and I'm having trouble >>>>>>> in >>>>>>> using the loop (for) >>>>>>> >>>>>>> >>>>>>> I need to read a column (c1), and for each value of this column, I >>>>>>> need >>>>>>> to >>>>>>> check if it's within the control limits >>>>>>> >>>>>>> >>>>>>> So, I was trying to do this: >>>>>>> >>>>>>> >>>>>>> For (k in 1: c1) >>>>>>> >>>>>>> >>>>>>> If (c1< lcl1 | c1 > ucl1) {here I need to store the values outside the >>>>>>> limits) >>>>>>> >>>>>>> >>>>>>> I have 5 columns, need to do the same process in each one of them. >>>>>>> >>>>>>> >>>>>>> And later on I'm gonna concatenate these 5 vectors and calculate its >>>>>>> mean >>>>>>> for an ARL project. >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>>> [[alternative HTML version deleted]] >>>>>>> >>>>>>> ______________________________******________________ >>>>>>> R-help at r-project.org mailing list >>>>>>> https://stat.ethz.ch/mailman/******listinfo/r-help<https://stat.ethz.ch/mailman/****listinfo/r-help> >>>>>>> <https://**stat.ethz.ch/mailman/****listinfo/r-help<https://stat.ethz.ch/mailman/**listinfo/r-help> >>>>>>> <https://stat.**ethz.ch/**mailman/listinfo/r-**help<http://ethz.ch/mailman/listinfo/r-**help> >>>>>>> <http**s://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >>>>>>> PLEASE do read the posting guide http://www.R-project.org/** >>>>>>> posting-guide.html <http://www.R-project.org/****posting-guide.html<http://www.R-project.org/**posting-guide.html> >>>>>>> <http://www.**R-project.org/posting-guide.**html<http://www.R-project.org/posting-guide.html> >>>>>>> and provide commented, minimal, self-contained, reproducible code. >>>>>>> >>>>>>> >>>>>>> >