Hi all, I am pretty new to R and even new to programming in general. Right now i get only one value for j below (print(j)), how do i save all j values to one vector or matrix? Sorry for this very basic question. thanks in advance. wave2 <- abs(Re(rnorm(100))) i <- findInterval((wave2[1]), wave2) i <- i+1 while (i < length(wave2)) { j <- (wave2 [i] - wave2[i-1]) print(j) i<-i+1 } abid ____________________________________________________________________________________ Looking for last minute shopping deals?
I solved it myself by reading the help files which i should have done prior to my posting. Solution: wave2 <- abs(Re(rnorm(100))) wave2 <- sort (wave2, decreasing=F) hist(wave2) x <- length (wave2) i <- findInterval((wave2[1]), wave2) i <- i+1 out <- numeric(x) for (i in i:x) { out[i] <- (wave2 [i] - wave2[i-1]) } out As you see i trimmed the code and alloted a new variable called out which i later save my values to. --- Syed Abid Hussaini <ohitsabid at yahoo.com> wrote:> Hi all, > I am pretty new to R and even new to programming in general. Right now i get only one value > for > j below (print(j)), how do i save all j values to one vector or matrix? Sorry for this very > basic > question. thanks in advance. > > wave2 <- abs(Re(rnorm(100))) > i <- findInterval((wave2[1]), wave2) > i <- i+1 > > while (i < length(wave2)) { > j <- (wave2 [i] - wave2[i-1]) > print(j) > i<-i+1 > } > > abid > > > > ____________________________________________________________________________________ > Looking for last minute shopping deals?> http://tools.search.yahoo.com/newsearch/category.php?category=shopping >____________________________________________________________________________________ Looking for last minute shopping deals?
Its not clear what your code is intended to do but - put set.seed(123) before your code so you can run it reproducibly. - wave2 <- sort(abs(rnorm(100))) is the same as your first two lines - your findInterval line always returns 1 so its probably not what you want - i in i:x is confusing. Use j in i:x or even better use diff(wave2). On Dec 24, 2007 1:54 AM, Syed Abid Hussaini <ohitsabid at yahoo.com> wrote:> I solved it myself by reading the help files which i should have done prior to my posting. > > Solution: > > wave2 <- abs(Re(rnorm(100))) > wave2 <- sort (wave2, decreasing=F) > hist(wave2) > x <- length (wave2) > i <- findInterval((wave2[1]), wave2) > i <- i+1 > out <- numeric(x) > for (i in i:x) { > out[i] <- (wave2 [i] - wave2[i-1]) > } > out > > As you see i trimmed the code and alloted a new variable called out which i later save my values > to. > > > > --- Syed Abid Hussaini <ohitsabid at yahoo.com> wrote: > > > Hi all, > > I am pretty new to R and even new to programming in general. Right now i get only one value > > for > > j below (print(j)), how do i save all j values to one vector or matrix? Sorry for this very > > basic > > question. thanks in advance. > > > > wave2 <- abs(Re(rnorm(100))) > > i <- findInterval((wave2[1]), wave2) > > i <- i+1 > > > > while (i < length(wave2)) { > > j <- (wave2 [i] - wave2[i-1]) > > print(j) > > i<-i+1 > > } > > > > abid > > > > > > > > ____________________________________________________________________________________ > > Looking for last minute shopping deals? > > > http://tools.search.yahoo.com/newsearch/category.php?category=shopping > > > > > > ____________________________________________________________________________________ > Looking for last minute shopping deals? > > ______________________________________________ > 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. >
Thanks Gabor! I never realized there could be a "diff" function. all i was trying to do was to calculate the iterated differences in a vector. I feel so embarassed, but so happy for using R. by the way, i used "wave2 <- sort (wave2, decreasing=F)" to sort the values non-decreasingly because the code "i <- findInterval((wave2[1]), wave2)" doesnt work without this sorting. But all that is past now since i found the diff function. Thanks again. abid> > From: "Gabor Grothendieck" <ggrothendieck at gmail.com> > CC: R-help at stat.math.ethz.ch > To: "Syed Abid Hussaini" <ohitsabid at yahoo.com> > Date: Mon, 24 Dec 2007 02:30:57 -0500 > Subject: Re: [R] saving while loop values to one vector > > Its not clear what your code is intended to do but > > - put set.seed(123) before your code so you can > run it reproducibly. > > - wave2 <- sort(abs(rnorm(100))) is the same as your first > two lines > > - your findInterval line always returns 1 so its probably not what you want > > - i in i:x is confusing. Use j in i:x or even better use diff(wave2). > > On Dec 24, 2007 1:54 AM, Syed Abid Hussaini <ohitsabid at yahoo.com> wrote: > > I solved it myself by reading the help files which i should have done prior to my posting. > > > > Solution: > > > > wave2 <- abs(Re(rnorm(100))) > > wave2 <- sort (wave2, decreasing=F) > > hist(wave2) > > x <- length (wave2) > > i <- findInterval((wave2[1]), wave2) > > i <- i+1 > > out <- numeric(x) > > for (i in i:x) { > > out[i] <- (wave2 [i] - wave2[i-1]) > > } > > out > > > > As you see i trimmed the code and alloted a new variable called out which i later save my > values > > to. > > > > > > > > --- Syed Abid Hussaini <ohitsabid at yahoo.com> wrote: > > > > > Hi all, > > > I am pretty new to R and even new to programming in general. Right now i get only one > value > > > for > > > j below (print(j)), how do i save all j values to one vector or matrix? Sorry for this very > > > basic > > > question. thanks in advance. > > > > > > wave2 <- abs(Re(rnorm(100))) > > > i <- findInterval((wave2[1]), wave2) > > > i <- i+1 > > > > > > while (i < length(wave2)) { > > > j <- (wave2 [i] - wave2[i-1]) > > > print(j) > > > i<-i+1 > > > } > > > > > > abid > > > > > > > > >____________________________________________________________________________________ Looking for last minute shopping deals?
On Dec 24, 2007 1:08 PM, Syed Abid Hussaini <ohitsabid at yahoo.com> wrote:> Thanks Gabor! > I never realized there could be a "diff" function. all i was trying to do was to calculate the > iterated differences in a vector. I feel so embarassed, but so happy for using R. > by the way, i used > "wave2 <- sort (wave2, decreasing=F)" to sort the values non-decreasingly because the code "i <-That is the default for sort. Its not necessary to specify defaults. Also its better practice to write out F as FALSE since F can be a variable in R but FALSE cannot be.> findInterval((wave2[1]), wave2)" doesnt work without this sorting. > But all that is past now since i found the diff function. > > Thanks again. > > abid > > > > From: "Gabor Grothendieck" <ggrothendieck at gmail.com> > > CC: R-help at stat.math.ethz.ch > > To: "Syed Abid Hussaini" <ohitsabid at yahoo.com> > > Date: Mon, 24 Dec 2007 02:30:57 -0500 > > Subject: Re: [R] saving while loop values to one vector > > > > > Its not clear what your code is intended to do but > > > > - put set.seed(123) before your code so you can > > run it reproducibly. > > > > - wave2 <- sort(abs(rnorm(100))) is the same as your first > > two lines > > > > - your findInterval line always returns 1 so its probably not what you want > > > > - i in i:x is confusing. Use j in i:x or even better use diff(wave2). > > > > On Dec 24, 2007 1:54 AM, Syed Abid Hussaini <ohitsabid at yahoo.com> wrote: > > > I solved it myself by reading the help files which i should have done prior to my posting. > > > > > > Solution: > > > > > > wave2 <- abs(Re(rnorm(100))) > > > wave2 <- sort (wave2, decreasing=F) > > > hist(wave2) > > > x <- length (wave2) > > > i <- findInterval((wave2[1]), wave2) > > > i <- i+1 > > > out <- numeric(x) > > > for (i in i:x) { > > > out[i] <- (wave2 [i] - wave2[i-1]) > > > } > > > out > > > > > > As you see i trimmed the code and alloted a new variable called out which i later save my > > values > > > to. > > > > > > > > > > > > --- Syed Abid Hussaini <ohitsabid at yahoo.com> wrote: > > > > > > > Hi all, > > > > I am pretty new to R and even new to programming in general. Right now i get only one > > value > > > > for > > > > j below (print(j)), how do i save all j values to one vector or matrix? Sorry for this very > > > > basic > > > > question. thanks in advance. > > > > > > > > wave2 <- abs(Re(rnorm(100))) > > > > i <- findInterval((wave2[1]), wave2) > > > > i <- i+1 > > > > > > > > while (i < length(wave2)) { > > > > j <- (wave2 [i] - wave2[i-1]) > > > > print(j) > > > > i<-i+1 > > > > } > > > > > > > > abid > > > > > > > > > > > > > > > > ____________________________________________________________________________________ > Looking for last minute shopping deals? > Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping >