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
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
why not use this:
f <- function(v,i){
v.new <- v + 10
v.new[i] <- v[i]
v.new
}
###########
f(1:10,4)
f(1:10,8)
f(1:10,1)
f(1:10,10)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Robin Hankin" <r.hankin at soc.soton.ac.uk>
To: <R-help at stat.math.ethz.ch>
Sent: Friday, February 25, 2005 2:52 PM
Subject: [R] restoring vector v from v[-i]
> 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 at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
From: Robin Hankin <r.hankin at soc.soton.ac.uk>> > 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)])) > } >f <- function(v, i) replace(v+10, i, v[i])