Re: [R] How to subtract the counter i in for loop?
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 __ R-help@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.
[R] How to subtract the counter i in for loop?
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@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.
Re: [R] How to subtract the counter i in for loop?
Mike, Based on this example, what do you want samples to look like? It's not clear to me what you're trying to do with i-1 Jean C W tmrs...@gmail.com wrote on 11/29/2012 03:55:12 PM: 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@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.
Re: [R] How to subtract the counter i in for loop?
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 NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. C W tmrs...@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@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. __ R-help@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.
Re: [R] How to subtract the counter i in for loop?
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 __ R-help@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.
Re: [R] How to subtract the counter i in for loop?
Hi Jean, I am going through vector x, if it is less than 200, fill it into samples. I am having trouble with indexing x. For example, samples -rep(NA, 10) at i=2 x[2], 225200 is not true; and samples[2] remains NA. The loop continues to i=3, leaving x[2]=NA. I don't want that to happen, I want to fill up x[2] with value, in our case it's 111. Mike On Thu, Nov 29, 2012 at 5:39 PM, Jean V Adams jvad...@usgs.gov wrote: Mike, Based on this example, what do you want samples to look like? It's not clear to me what you're trying to do with i-1 Jean C W tmrs...@gmail.com wrote on 11/29/2012 03:55:12 PM: 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@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.
Re: [R] How to subtract the counter i in for loop?
I want to fill up vector a with vector b. The condition is: b200, otherwise don't fill it up. Mike On Thu, Nov 29, 2012 at 6:11 PM, David Winsemius dwinsem...@comcast.netwrote: 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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to subtract the counter i in for loop?
Mike, I'm still not sure what you want samples to look like, but here are a couple possibilities, using a different x. library(zoo) x - c(50, 250, 100, 300, 300, 20, 40, 800) samples1 - ifelse(x200, x, NA) samples2 - na.locf(samples1) samples3 - samples1[!is.na(samples1)] samples1 samples2 samples3 samples1 [1] 50 NA 100 NA NA 20 40 NA samples2 [1] 50 50 100 100 100 20 40 40 samples3 [1] 50 100 20 40 Jean C W tmrs...@gmail.com wrote on 11/29/2012 05:16:49 PM: Hi Jean, I am going through vector x, if it is less than 200, fill it into samples. I am having trouble with indexing x. For example, samples -rep(NA, 10) at i=2 x[2], 225200 is not true; and samples[2] remains NA. The loop continues to i=3, leaving x[2]=NA. I don't want that to happen, I want to fill up x[2] with value, in our case it's 111. Mike On Thu, Nov 29, 2012 at 5:39 PM, Jean V Adams jvad...@usgs.gov wrote: Mike, Based on this example, what do you want samples to look like? It's not clear to me what you're trying to do with i-1 Jean C W tmrs...@gmail.com wrote on 11/29/2012 03:55:12 PM: 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@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.
Re: [R] How to subtract the counter i in for loop?
On Nov 29, 2012, at 3:25 PM, C W wrote: I want to fill up vector a with vector b. Vector a? Vector b? The condition is: b200, otherwise don't fill it up. That would seem to be the current behavior, subject of course to an agreed upon definition for filling up. It's possible , even likely, that you can get what you want with no loop at all. samples[ x 200 ] - x[ x 200 ] -- David. Mike On Thu, Nov 29, 2012 at 6:11 PM, David Winsemius dwinsem...@comcast.net wrote: 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 David Winsemius, MD Alameda, CA, USA [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to subtract the counter i in for loop?
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 thanks, Mike On Thu, Nov 29, 2012 at 7:55 PM, David Winsemius dwinsem...@comcast.netwrote: On Nov 29, 2012, at 3:25 PM, C W wrote: I want to fill up vector a with vector b. Vector a? Vector b? The condition is: b200, otherwise don't fill it up. That would seem to be the current behavior, subject of course to an agreed upon definition for filling up. It's possible , even likely, that you can get what you want with no loop at all. samples[ x 200 ] - x[ x 200 ] -- David. Mike On Thu, Nov 29, 2012 at 6:11 PM, David Winsemius dwinsem...@comcast.netwrote: 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 David Winsemius, MD Alameda, CA, USA [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to subtract the counter i in for loop?
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[x200] or samples - x[which(x200)] 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 __ R-help@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.
Re: [R] How to subtract the counter i in for loop?
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()? Mike On Fri, Nov 30, 2012 at 12:24 AM, Berend Hasselman b...@xs4all.nl wrote: 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[x200] or samples - x[which(x200)] 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 [[alternative HTML version deleted]] __ R-help@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.