Skip to content

filling an array

5 messages · Jannetta Steyn, Jim Lemon, Joshua Wiley +1 more

#
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
#
Hi Jannetta,

Try this:

rep(c(0, v), each = 10)

See ?rep for details

Cheers,

Josh
On Sat, Feb 23, 2013 at 7:56 PM, Jannetta Steyn <jannetta at henning.org> wrote:
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
#
Hi,

The usual way is

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

Best,
Ista
On Sat, Feb 23, 2013 at 10:56 PM, Jannetta Steyn <jannetta at henning.org> wrote: