[R] How to add values to an array at any position.

Henrik Bengtsson hb at maths.lth.se
Sun Oct 31 09:47:40 CET 2004


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Witold 
> Eryk Wolski
> Sent: Sunday, October 31, 2004 1:15 AM
> To: Peter Dalgaard
> Cc: R Help Mailing List
> Subject: Re: [R] How to add values to an array at any position.
> 
> 
> Hi,
> 
> In the first place I was wondering if there are no function 
> _insert.vector_  in base.

I have an insert() in my R.basic package (to install, see
http://www.maths.lth.se/help/R/R.classes/);

Usage: 
insert(x, index, value=NA)

Example:
vec     <- c(1:10,13:15,19:20)
missing <- setdiff(1:20, vec)
vec2 <- insert(vec, missing, value=NA)
print(vec2)
# [1]  1  2  3  4  5  6  7  8  9 10 NA NA 13 14 15 NA NA NA 19 20

Is this what you're looking for?

Henrik


 
> Thanks Peter and Gabor even for the "bad news".
> 
> E.
> 
> 
> Peter Dalgaard wrote:
> 
> >Witold Eryk Wolski <wolski at molgen.mpg.de> writes:
> >
> >  
> >
> >>Hi,
> >>
> >>How to add values to an array at any position.
> >>Asking because of the following:
> >>e.g.
> >>
> >>y<-c(0.1,NaN,0.2,NaN) #or data frame
> >>
> >>
> >>x<-na.omit(y)
> >>
> >>take some columns from x and
> >>do some computation with functions which do not allow NaN 's.
> >>
> >>After the computing add NaN's at positions stored in
> >>attr(x,"na.action")
> >>of the result vector.
> >>    
> >>
> >
> >You mean, something like
> >
> >ix <- attr(x, "na.action")
> >newres <- c(res,rep(NA,length(ix))) # just to get size and 
> mode right 
> >newres[ix] <- NA newres[-ix] <- res
> >
> >  
> >
> 
> 
> -- 
> Dipl. bio-chem. Witold Eryk Wolski
> MPI-Moleculare Genetic
> Ihnestrasse 63-73 14195 Berlin
> tel: 0049-30-83875219                 __("<    _
> http://www.molgen.mpg.de/~wolski      \__/    'v'
> http://r4proteomics.sourceforge.net    ||    /   \
> mail: witek96 at users.sourceforge.net    ^^     m m
>       wolski at molgen.mpg.de
> 
> ______________________________________________
> 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