Skip to content
Prev 318208 / 398503 Next

filling an array

On 02/24/2013 02:56 PM, Jannetta Steyn wrote:
Hi Janetta,
It makes some sense, but perhaps not enough. The easy answer would be:

I<-c(rep(0,10),rep(v,10))

If you mean you want _alternating_ values:

I<-rep(c(0,14),10)

If you want a general solution to the problem of filling a vector of 
arbitrary length with alternating values:

fill_alternating<-function(len=2,value1=0,value2=1) {
  return(rep(c(value1,value2),(len+1)/2)[1:len])
}

There are probably other interpretations as well.

Jim