0

I want to create the columns below wherein I can specify the range to apply the value 1 to one column and the rest 0. I know this has been asked and answered perfectly here before but I can't find that particular one right now.

    time1   time2   time3   time4   time5
1       1       0       0       0       0
2       1       0       0       0       0
3       1       0       0       0       0
4       1       0       0       0       0
5       1       0       0       0       0
6       0       1       0       0       0
7       0       1       0       0       0
8       0       1       0       0       0
9       0       1       0       0       0
10      0       1       0       0       0
11      0       0       1       0       0
12      0       0       1       0       0
13      0       0       1       0       0
14      0       0       1       0       0
15      0       0       1       0       0
16      0       0       0       1       0
17      0       0       0       1       0
18      0       0       0       1       0
19      0       0       0       1       0
20      0       0       0       1       0
21      0       0       0       0       1
22      0       0       0       0       1
23      0       0       0       0       1
24      0       0       0       0       1
25      0       0       0       0       1

I can't recall how this was generated but the answer included an n option to specify the intervals per column.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485

2 Answers2

3

You could do

cols <- 4
diag(cols)[rep(1:cols, each=cols), ]

      [,1] [,2] [,3] [,4]
 [1,]    1    0    0    0
 [2,]    1    0    0    0
 [3,]    1    0    0    0
 [4,]    1    0    0    0
 [5,]    0    1    0    0
 [6,]    0    1    0    0
 [7,]    0    1    0    0
 [8,]    0    1    0    0
 [9,]    0    0    1    0
[10,]    0    0    1    0
[11,]    0    0    1    0
[12,]    0    0    1    0
[13,]    0    0    0    1
[14,]    0    0    0    1
[15,]    0    0    0    1
[16,]    0    0    0    1
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
0

I finally found the exact question: "R add new column with predefined pattern" and the solution I was looking for is:

range <- 5
cols <- 5
y <- gl(cols, range)
mat <- model.matrix(~y-1) # -1 is for remove the intercept
colnames(mat) <- paste0('var', 1:cols)
mat

It provides a dataframe ready to be included in other dataframes (via merge(old, new)) and specifying a name for the columns.

The output is:

   var1 var2 var3 var4 var5
1     1    0    0    0    0
2     1    0    0    0    0
3     1    0    0    0    0
4     1    0    0    0    0
5     1    0    0    0    0
6     0    1    0    0    0
7     0    1    0    0    0
8     0    1    0    0    0
9     0    1    0    0    0
10    0    1    0    0    0
11    0    0    1    0    0
12    0    0    1    0    0
13    0    0    1    0    0
14    0    0    1    0    0
15    0    0    1    0    0
16    0    0    0    1    0
17    0    0    0    1    0
18    0    0    0    1    0
19    0    0    0    1    0
20    0    0    0    1    0
21    0    0    0    0    1
22    0    0    0    0    1
23    0    0    0    0    1
24    0    0    0    0    1
25    0    0    0    0    1
Community
  • 1
  • 1