[R] restoring vector v from v[-i]
Robin Hankin
r.hankin at soc.soton.ac.uk
Fri Feb 25 14:52:44 CET 2005
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
More information about the R-help
mailing list