Hi, I am the first R user, I have met some problem and I am seeking for help. I am estimating the wealth of a shareholder. First, I simulated 10000 set of price, each set of price contains 5200 prices which reflect the price goes up and down through out the year. Price # consists of all set the price After that, I use each set of price to calculate the wealth for each sample (10000) and save the data in a character, so, I do this : q <- 0.9 Wealth <- list() for (s in 1:10000) { for (i in 1:5200){ Ca[i+1] <- Cash(i,q,Price[[s]][i+1]) Sh[i+1] <- Share(i,q,Price[[s]][i+1]) Po[i+1] <- Position(i,q,Price[[s]][i+1]) Wealth[[s]][i] <- Ca[i+1]+Po[i+1] }} *note = Ca, Sh, and Po are the vectors with length of 5200, and the Cash, Share and Position are functions that I created by myself. I am very sure that these can be work properly. Ca, Sh and Po will change when it use difference set of price ( I think). After I run them, it gave the error message: Error in `*tmp*`[[s]] : subscript out of bounds I could run all this when I had just 1 set of price : for (i in 1:5200){ Ca[i+1] <- Cash(i,q,Price[i+1]) Sh[i+1] <- Share(i,q,Price[i+1]) Po[i+1] <- Position(i,q,Price[i+1]) Wealth[[s]][i] <- Ca[i+1]+Po[i+1] } So, I wonder the error occurs on that I use the double loops in improper way or the data I save in incorrect way. hope you can understand what I am asking and I appreciate that you answer my question. -- View this message in context: http://n4.nabble.com/using-double-loops-and-saving-the-data-tp1836591p1836591.html Sent from the R help mailing list archive at Nabble.com.
On Apr 11, 2010, at 8:23 PM, ChinChin wrote:> > Hi, > > I am the first R user, I have met some problem and I am seeking for > help. > > I am estimating the wealth of a shareholder. > First, I simulated 10000 set of price, each set of price contains 5200 > prices which reflect the price goes up and down through out the year. > > Price # consists of all set the price > > After that, I use each set of price to calculate the wealth for each > sample > (10000) and save the data in a character, so, I do this : > > q <- 0.9 > Wealth <- list() > for (s in 1:10000) { > for (i in 1:5200){ > Ca[i+1] <- Cash(i,q,Price[[s]][i+1])I am guessing that the first time through when i= 5200 that i+1 is indexing an entry that does not exist. What does str( Price[[1]] [5200+1] ) return? What about str(Ca)?> Sh[i+1] <- Share(i,q,Price[[s]][i+1]) > Po[i+1] <- Position(i,q,Price[[s]][i+1]) > Wealth[[s]][i] <- Ca[i+1]+Po[i+1] > }} > > *note = Ca, Sh, and Po are the vectors with length of 5200,So what is supposed to happen when you try "Ca[5200+1] <- "... anything?> and the Cash, > Share and Position are functions that I created by myself. I am very > sure > that these can be work properly. > Ca, Sh and Po will change when it use difference set of price ( I > think). > > After I run them, it gave the error message: Error in `*tmp*`[[s]] : > subscript out of bounds > > I could run all this when I had just 1 set of price : > for (i in 1:5200){ > Ca[i+1] <- Cash(i,q,Price[i+1])Really? I thought you said you had 5200 entries in Price?> Sh[i+1] <- Share(i,q,Price[i+1]) > Po[i+1] <- Position(i,q,Price[i+1]) > Wealth[[s]][i] <- Ca[i+1]+Po[i+1] > } > > So, I wonder the error occurs on that I use the double loops in > improper way > or the data I save in incorrect way.You need to read the error message. And you need to offer better information about the objects you are working with. -- David.> > hope you can understand what I am asking and I appreciate that you > answer my > question. > -- > View this message in context: http://n4.nabble.com/using-double-loops-and-saving-the-data-tp1836591p1836591.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
David Winsemius wrote:> > > >>I am guessing that the first time through when i= 5200 that i+1 is >>indexing an entry that does not exist. What does str( Price[[1]] >>[5200+1] ) return? What about str(Ca)? > > >>So what is supposed to happen when you try "Ca[5200+1] <- "... anything? > >>Really? I thought you said you had 5200 entries in Price? > > I am sorry, I should mention it earlier that there are initial numbers for > Ca, Sh, Po and Price. Which means there are totally 5200+1 entries for > each of them. > I can get the answer that I want from str(Price[[1]][5200+1]) > but str(Ca) gave me all zeros "num [1:5201] 0 0 0 0 0 0 0 0 0 0 ..." > Ca[5200+1] = 0 > >>You need to read the error message. And you need to offer better >>information about the objects you are working with. > > -- >>David. > > View this message in context: > http://n4.nabble.com/using-double-loops-and-saving-the-data-tp1836591p1836591.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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. > >-- View this message in context: http://n4.nabble.com/using-double-loops-and-saving-the-data-tp1836591p1836690.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 12.04.2010 07:14:14:> > > David Winsemius wrote: > > > > > > > >>I am guessing that the first time through when i= 5200 that i+1 is > >>indexing an entry that does not exist. What does str( Price[[1]] > >>[5200+1] ) return? What about str(Ca)? > > > > > >>So what is supposed to happen when you try "Ca[5200+1] <- "...anything?> > > >>Really? I thought you said you had 5200 entries in Price? > > > > I am sorry, I should mention it earlier that there are initial numbersfor> > Ca, Sh, Po and Price. Which means there are totally 5200+1 entries for > > each of them. > > I can get the answer that I want from str(Price[[1]][5200+1]) > > but str(Ca) gave me all zeros "num [1:5201] 0 0 0 0 0 0 0 0 0 0 ..." > > Ca[5200+1] = 0You did not tell much more about your data and procedures. Each object type has some distinct way of indexing and you can not mix them up. x<-1:10> x[5][1] 5> x[[5]][1] 5> x[5,]Error in x[5, ] : incorrect number of dimensions> x<-list(1:10) > x[1][[1]] [1] 1 2 3 4 5 6 7 8 9 10> x[5][[1]] NULL> x[[5]]Error in x[[5]] : subscript out of bounds> x[1][5][[1]] NULL> x[[1]][5][1] 5 Nobody except you has your data available, so without providing more clues you can not expect mor relevant answers. Try str(your.objects) and maybe you could use debug to see how they are operating and changing through a cycle. Regards Petr> > > >>You need to read the error message. And you need to offer better > >>information about the objects you are working with. > > > > -- > >>David. > > > > View this message in context: > >http://n4.nabble.com/using-double-loops-and-saving-the-data-tp1836591p1836591.html> >> Sent from the R help mailing list archive at Nabble.com. > >> > >> ______________________________________________ > >> 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. > > > > ______________________________________________ > > 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. > > > > > > -- > View this message in context:http://n4.nabble.com/using-double-loops-and-> saving-the-data-tp1836591p1836690.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.