[R] modifynig some elements of a vector
Eric Elguero
Eric.Elguero at ird.fr
Thu Feb 10 12:50:03 CET 2011
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
More information about the R-help
mailing list