0

I am trying to create a regular sequence of numbers that are alternating between an increasing integer and zeros. e.g.

0,1,0,2,0,3,0,4 ... up to some value that I need functionally specify.

2 Answers2

2

You can use this.

n = 10
as.vector(rbind(rep(0,n), (1:n)))

Result:

0  1  0  2  0  3  0  4  0  5  0  6  0  7  0  8  0  9  0 10
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
1

Function f(n) gives you the result you are after:

f <- function(n){
  i <- 1:(2*n)
  ifelse(i %% 2, 0, i/2)
}

f(5)
# 0 1 0 2 0 3 0 4 0 5
Constantinos
  • 1,327
  • 7
  • 17