should be able to: u <- unique(u) x[u] <- x[u] + 1
On Thu, Feb 10, 2011 at 6:50 AM, Eric Elguero <eric.elgu...@ird.fr> wrote: > He everybody, > > I want to add 1 to some elements of a vector: > > x is a vector > u is a vector of idices, that is, integers > assumed to be within the range 1..length(x) > and I want to add 1 to the elements of x > each time their index appears in u > > x[u]<-x[u]+1 works only when there are no > duplicated values in u > > I found this solution: > > tu <- table(u) > indices <- as.numeric(names(tu)) > x[indices] <- x[indices]+tu > > but it looks ugly to me and I would > prefer to avoid calling the function 'table' > since this is to be done millions of times > as part of a simulation program. > > Eric Elguero > Génétique & Adaptation des Plasmodium > IRD > Montpellier - FRance > > ______________________________________________ > 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. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? ______________________________________________ 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.