I have a table (t1) as follows:
t1 <- array(1:20, dim=c(10,10))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 1 11 1 11 1 11 1 11
[2,] 2 12 2 12 2 12 2 12 2 12
[3,] 3 13 3 13 3 13 3 13 3 13
[4,] 4 14 4 14 4 14 4 14 4 14
[5,] 5 15 5 15 5 15 5 15 5 15
[6,] 6 16 6 16 6 16 6 16 6 16
[7,] 7 17 7 17 7 17 7 17 7 17
[8,] 8 18 8 18 8 18 8 18 8 18
[9,] 9 19 9 19 9 19 9 19 9 19
[10,] 10 20 10 20 10 20 10 20 10 20
I want to transform this table to either 1 or 0. If the cells number is >5, we give it a 1, if the cells number is <5 or = 5, we give it a 0. Thus after transformation, table t1 will become the followings:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 0 1 0 1 0 1 0 1
[2,] 0 1 0 1 0 1 0 1 0 1
[3,] 0 1 0 1 0 1 0 1 0 1
[4,] 0 1 0 1 0 1 0 1 0 1
[5,] 0 1 0 1 0 1 0 1 0 1
[6,] 1 1 1 1 1 1 1 1 1 1
[7,] 1 1 1 1 1 1 1 1 1 1
[8,] 1 1 1 1 1 1 1 1 1 1
[9,] 1 1 1 1 1 1 1 1 1 1
[10,] 1 1 1 1 1 1 1 1 1 1
Which commands should I used?
You can just use an
ifelse()statement:Since a statement like
t1 > 5will result in a matrix ofTRUEandFALSE, and since R treatsTRUEas “1” andFALSEas “0“, for this particular transformation, you could also do: