Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

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

Robin Hankin

2005-02-25

Replies:

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)]))
}

(my example is adding 10 to v[-i], but the real version is the solution
of a complicated function involving
a call to Solve(A,b))


Function f() works most of the time:
> f(1:10,4)
[1] 11 12 13 4 15 16 17 18 19 20
> f(1:10,8)
[1] 11 12 13 14 15 16 17 8 19 20


but fails at the "edges":

f(1:10,1)
[1] 12 1 12 13 14 15 16 17 18 19 20
> f(1:10,10)
[1] 11 12 13 14 15 16 17 18 19 10 NA 19
>


[ I would want f(1:10,1) to start 1,12,13,...  and f(1:10,10) to end
. . . 18 19,10]


How best to get intended behaviour for i=1 and i=length(v)?




--
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.