[R] restoring vector v from v[-i]

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri Feb 25 15:14:45 CET 2005


Robin Hankin wrote:
> Hi
> 
> I have a little function that takes a vector v and an integer i.  I want 
> to manipulate
> v[-i] (to give v.new, say) and
> then put (unmanipulated)  v[i] back into the appropriate place.  For 
> example,
> 
> 
> f <- function(v,i){
> v.new <- v[-i]+10
> return(c(v.new[1:(i-1)],v[i],v.new[i:length(v.new)]))
> }

  Take the complement of 'i', and change the values of v in place?

f <- function(v,i){
   v[-i] <- v[-i]+10
   v
}

  Now i can be an integer or a vector of integers or true/falses.

Baz




More information about the R-help mailing list