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

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Fri Feb 25 15:20:49 CET 2005


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
>




More information about the R-help mailing list