Inserting new values into a vector requires to describe
where to insert the new values. The function "insert.values"
allows you to define the new position(s) by fractional
indices or by a logical vector.

"insert.values" <- function(x,pos.insert,x.insert){
# insert.values inserts x.insert into x at positions defined by
pos.insert
# Arguments:
#   x           input vector
#   x.insert    vector of new values to be stored
#   pos.insert  defines the positions of the new values in one of two
ways:
#               a) pos.insert is a vector of fractional numbers of the
same length
#                  as x.insert: x.insert[i] will be inserted between
#                  x[ floor(pos.insert) ] and x[ ceiling(pos.insert) ]
#               b) pos.insert is of mode logical with length(x) values
FALSE
#                  and length(x.insert) values TRUE. Then TRUE values
indicate
#                  the position of the new elements and FALSE the values

#                  of old ones
#
# Examples:
#   > x<-1:5
#   > insert.values(x, 2.5, 1000)
#   [1]    1    2 1000    3    4    5
#   > insert.values(x, (1:5)+.2, 1001:1005)
#   [1]    1 1001    2 1002    3 1003    4 1004    5 1005
#   > insert.values(x, .9, 1111)
#   [1] 1111    1    2    3    4    5
#   > insert.values(x, c(T,rep(F,5)), 1000)
#   [1] 1000    1    2    3    4    5
#   > insert.values(x, c(rep(F,5),T), 1000)
#   [1]    1    2    3    4    5 1000
#   > insert.values(x, rbind(rep(F,5),T), 1001:1005)
#   [1]    1 1001    2 1002    3 1003    4 1004    5 1005
# pw 02/2003
  if(is.logical(pos.insert)){
    pos.insert <- pos.insert[pos.insert]/length(pos.insert) +
cumsum(!pos.insert)[pos.insert]
  }
  x<-c(x,x.insert)[order(c(seq(x),pos.insert))]
  return(x)
}

Peter Wolf
--------------------------------------
Dr. Agustin Lobo wrote:
> I've searched the doc for insert
> and could not find the way to do the following,
> hope someone can help:
>
> Let's say we have a vector:
> > a
> [1] "1" "2" "3" "5" "6" "3"
>
> and we want to insert a "7" after
> any given "3", i.e., we want vector a
> to become:
>
> [1] "1" "2" "3" "7" "5" "6" "3" "7"

-------------------------------------------------------------------------------
 Peter Wolf,   Statistik/Informatik,   Fak.f.Wiwi,   Uni Bielefeld
 pwolf@wiwi.uni-bielefeld.de, http://www.wiwi.uni-bielefeld.de/~wolf/wolf.html
-------------------------------------------------------------------------------



	[[alternate HTML version deleted]]


