I want to add values onto the end of a vector, for example... x <- vector for (i in 1:5){ add_to_end_of_vector(i,x) } I just cant find the answer to this question! Sorry for such a basic question, I tried... x <- c() for (i in 1:5) x[length(x)] <- i but it didn't work.
Is this what you're looking for?> x <- numeric(0) > for (i in 1:5) x <- append(x, i) > x[1] 1 2 3 4 5 Andy> From: Dan Bolser > > I want to add values onto the end of a vector, for example... > > x <- vector > > for (i in 1:5){ > add_to_end_of_vector(i,x) > } > > I just cant find the answer to this question! > > > Sorry for such a basic question, I tried... > > x <- c() > > for (i in 1:5) x[length(x)] <- i > > but it didn't work. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
The following works, but there may be more efficient ways to do this ...> x=numeric(0) > for(i in 1:5) {+ x[length(x)+1]=i + }> x[1] 1 2 3 4 5>Best, ingmar On 1/4/05 2:41 PM, "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> wrote:> > I want to add values onto the end of a vector, for example... > > x <- vector > > for (i in 1:5){ > add_to_end_of_vector(i,x) > } > > I just cant find the answer to this question! > > > Sorry for such a basic question, I tried... > > x <- c() > > for (i in 1:5) x[length(x)] <- i > > but it didn't work. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Tue, 4 Jan 2005, Dan Bolser wrote:> > I want to add values onto the end of a vector, for example... > > x <- vector > > for (i in 1:5){ > add_to_end_of_vector(i,x) > } >?append> I just cant find the answer to this question! > > > Sorry for such a basic question, I tried... > > x <- c() > > for (i in 1:5) x[length(x)] <- i > > but it didn't work. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no