2

I'm trying to learn how to provide a reproducible R example in case I needed one in the future.

I use the SimSurv() function and I want to add six new columns (binary variables) to it. These columns' values should not be parallel or intersecting ie. var1 must have the value 1 for elements 1 to 100 and the rest 0; var2 will start at 101 to 200 and so on.

x = SimSurv(600)
var1 = matrix(0:1, nrow=100) # 1 - 100
var2 = matrix(0:1, nrow=100) # 101 - 200
...
var6 = matrix(0:1, nrow=100) # 501 - 600

I can not phrase this one into a legitimate Google search thus I asked here to expound my problem.

Community
  • 1
  • 1

2 Answers2

3

You can do it with gl() and model.matrix():

y <- gl(6, 100)
mat <- model.matrix(~y-1) # -1 is for remove the intercept
colnames(mat) <- paste0('var', 1:6)
mat
Rcoster
  • 3,170
  • 2
  • 16
  • 35
0

If I understand you correctly you can do this with rep and matrix like so:

m <- rep( rep( c( 1 , 0 ) , times = c( 100 , 600 ) ) , 6 )
matrix( m[1:3600] , nrow = 600 , byrow = FALSE )

On an example 100 times smaller this gives the following, non-intersecting binary variable columns:

m <- rep( rep( c( 1 , 0 ) , times = c( 1 , 6 ) ) , 6 )
matrix( m[1:36] , nrow = 6 , byrow = FALSE )
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1    0    0    0    0    0
#[2,]    0    1    0    0    0    0
#[3,]    0    0    1    0    0    0
#[4,]    0    0    0    1    0    0
#[5,]    0    0    0    0    1    0
#[6,]    0    0    0    0    0    1
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184