An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/ba166f8a/attachment.pl>
How to subtract the counter i in for loop?
12 messages · C W, Jeff Newmiller, Jean V Adams +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/04f9a968/attachment.pl>
Use a while loop instead of a for loop. I don't think what you have coded makes any sense, but fighting the for loop over control of the indexing variable is a recipe for failure.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
C W <tmrsg11 at gmail.com> wrote:
Hi list,
I am writing a for loop that looks like this:
samples<-rep(NA,10)
x <- rep(c(111, 225), 5)
for(i in 1:10){
If(x[i]<200){
samples[i] <- x[i]
}else{
i=i-1
}
}
The problem is that the returning vector still contains NA, I think
the i
in "else" is not getting subtracted. How should I get it to work?
Thanks,
Mike
[[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.
On Nov 29, 2012, at 1:55 PM, C W wrote:
Hi list,
I am writing a for loop that looks like this:
samples<-rep(NA,10)
x <- rep(c(111, 225), 5)
for(i in 1:10){
If(x[i]<200){
samples[i] <- x[i]
}else{
i=i-1
If you expected the else clause to assign something to the samples vector, you are mistaken.
} } The problem is that the returning vector still contains NA, I think the i in "else" is not getting subtracted. How should I get it to work?
You could start by telling us what you wanted to happen. You can change the index of a for loop inside the body, but it will not "back up the process" since at the end of the loop the next "i" will not depend on what you changed it to inside the loop.
David Winsemius, MD Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/86bb484f/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/71519cac/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/b884e336/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/54188d36/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121129/853e6dff/attachment.pl>
On 30-11-2012, at 05:47, C W wrote:
David, Your results is,
samples
[1] 111 NA 111 NA 111 NA 111 NA 111 NA It still has NA's in it. I want it look like this,
samples
[1] 111 111 111 111 111
You don't need the for loop at all.
samples <- x[x<200]
or
samples <- x[which(x<200)]
Your for loop should look something like this
k <- 1
for(i in 1:10){
if(x[i]<200){
samples[k] <- x[i]
k <- k+1
}
}
na.omit(samples)
Berend
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121130/f031cb77/attachment.pl>
On 30-11-2012, at 07:18, C W wrote:
thanks, Berend. Both of your code works great. Is there a function that can do it?
Something like this:
x <- matrix(NA, nrow=15, ncol=2)
for(i in 1:15){
x[i,] <- sample(c(NA, 20, 77), 2, prob=c(0.2, 0.3, 0.4))
}
x
[,1] [,2]
[1,] NA 77
[2,] 77 NA
[3,] NA 77
[4,] 77 20
[5,] 77 20
[6,] 77 20
[7,] 20 NA
[8,] 77 20
[9,] 77 NA
[10,] 77 NA
[11,] 77 20
[12,] 20 77
[13,] NA 77
[14,] 77 20
[15,] 77 20
I want to have a column of 15 samples without NA's. Is there an R function like ifelse()?
It's not clear what you exactly want. A matrix with 15 rows and some columns? How do you want to remove the NA's in each column? Then why don't you leave out the NA in the sample? Berend