Hi, How can I remove elements from a vector and them put them back in place?? An example (very simple, my vector/operations are much larger/complicated): Got a vector, lets say: a<-c(1,2,40,10,3,20,6); # I remove values larger than 10 a<-a[a<10] # Do some operations on the new a "1 2 3 6" b<-a^2 # Now b is "[1] 1 4 9 36" # Now I want to insert the elements I removed in a back into a and b # so I get: # a "1 2 40 10 3 20 6" #and b "1 4 40 10 9 20 36" The only thing I've found on the archives is explanations on how to insert: http://maths.newcastle.edu.au/~rking/R/help/03a/1794.html Thanks, Angel
On Thu, 17 Jul 2003, Angel wrote:> > Hi, > How can I remove elements from a vector and them put them back in place?? > An example (very simple, my vector/operations are much larger/complicated): > Got a vector, lets say: > a<-c(1,2,40,10,3,20,6);you need to save the index of the elements of interest: R> a<-c(1,2,40,10,3,20,6) R> indx <- which(a < 10) R> b <- a[indx] R> b <- b^2 R> a[indx] <- b R> a [1] 1 4 40 10 9 20 36 and similar with b Torsten> # I remove values larger than 10 > a<-a[a<10] > # Do some operations on the new a "1 2 3 6" > b<-a^2 > # Now b is "[1] 1 4 9 36" > # Now I want to insert the elements I removed in a back into a and b > # so I get: > # a "1 2 40 10 3 20 6" > #and b "1 4 40 10 9 20 36" > > The only thing I've found on the archives is explanations on how to insert: > http://maths.newcastle.edu.au/~rking/R/help/03a/1794.html > Thanks, > Angel > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
Angel wrote:> Hi, > How can I remove elements from a vector and them put them back in place?? > An example (very simple, my vector/operations are much larger/complicated): > Got a vector, lets say: > a<-c(1,2,40,10,3,20,6); > # I remove values larger than 10 > a<-a[a<10] > # Do some operations on the new a "1 2 3 6" > b<-a^2 > # Now b is "[1] 1 4 9 36" > # Now I want to insert the elements I removed in a back into a and b > # so I get: > # a "1 2 40 10 3 20 6" > #and b "1 4 40 10 9 20 36"In this case, you don't want to remove the elements, but calculate only on the others as in: b <- ifelse(a < 10, a^2, a) or b <- a b[b < 10] <- b[b < 10]^2 Uwe Ligges> The only thing I've found on the archives is explanations on how to insert: > http://maths.newcastle.edu.au/~rking/R/help/03a/1794.html > Thanks, > Angel > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On 17-Jul-03 Angel wrote:> Got a vector, lets say: > a<-c(1,2,40,10,3,20,6); ># I remove values larger than 10 > a<-a[a<10] ># Do some operations on the new a "1 2 3 6" > b<-a^2 ># Now b is "[1] 1 4 9 36" ># Now I want to insert the elements I removed in a back into a and b ># so I get: ># a "1 2 40 10 3 20 6" >#and b "1 4 40 10 9 20 36"For the above example, step-by-step:> a<-c(1,2,40,10,3,20,6); > ix<-(a<10) > b<-a[ix] > b<-b^2 > a[ix]<-b > a[1] 1 4 40 10 9 20 36 However, in this particuarly simple case it could easily be done in one go with a[a<10]<-(a[a<10])^2> a<-c(1,2,40,10,3,20,6); > a[a<10]<-(a[a<10])^2 > a[1] 1 4 40 10 9 20 36 But if what you want to do to these numbers is much more complicated then the previous (step-by-step) method may be best. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 17-Jul-03 Time: 12:30:19 ------------------------------ XFMail ------------------------------