Jenny persson
2006-Oct-22 15:38 UTC
[R] how to create a vector with different categories in a simple way?
An embedded and charset-unspecified text was scrubbed... Name: inte tillg?nglig Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061022/56a34662/attachment.pl
Marc Schwartz
2006-Oct-22 16:31 UTC
[R] how to create a vector with different categories in a simple way?
On Sun, 2006-10-22 at 17:38 +0200, Jenny persson wrote:> Hi R-users, > > I have a matrice called layout which contains 5 columns:id, name, > row, column and block. The column called "block" has totally 48 blocks > and looks like > > > 1 2 3 4 > 5 6 7 8 > 9 10 11 12 > 13 14 15 16 > 17 18 19 20 > 21 22 23 24 > 25 26 27 28 > 29 30 31 32 > 33 34 35 36 > 37 38 39 40 > 41 42 43 44 > 45 46 47 48 > > Each block (1-48) has 18 rows and 18 columns. I want to create 2 > variables called blockrow and blockcol in such a way that blockrow > will have value 1 for block 1,2 3 and 4, blockrow=2 for blocks 5,6,7 > and 8 and so on. Similarly, blockcol = 1 for blocks > 1,5 ,9,13,17,21,25,29,33,37,41 and 44 and so on. As you can see there > are 12 blockrows and 4 blockcols. I have written the following > programme but it didnot give the desirable output. How can I make it > work in a simplier way ?<snip of code> I may be totally misunderstanding what you want, but is the following close?> mat[,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 [4,] 13 14 15 16 [5,] 17 18 19 20 [6,] 21 22 23 24 [7,] 25 26 27 28 [8,] 29 30 31 32 [9,] 33 34 35 36 [10,] 37 38 39 40 [11,] 41 42 43 44 [12,] 45 46 47 48 blockrow <- row(mat) blockcol <- col(mat)> blockrow[,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 [3,] 3 3 3 3 [4,] 4 4 4 4 [5,] 5 5 5 5 [6,] 6 6 6 6 [7,] 7 7 7 7 [8,] 8 8 8 8 [9,] 9 9 9 9 [10,] 10 10 10 10 [11,] 11 11 11 11 [12,] 12 12 12 12> blockcol[,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 1 2 3 4 [3,] 1 2 3 4 [4,] 1 2 3 4 [5,] 1 2 3 4 [6,] 1 2 3 4 [7,] 1 2 3 4 [8,] 1 2 3 4 [9,] 1 2 3 4 [10,] 1 2 3 4 [11,] 1 2 3 4 [12,] 1 2 3 4 And you can then coerce them to vectors, if that is what you want:> as.vector(blockcol)[1] 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 [35] 3 3 4 4 4 4 4 4 4 4 4 4 4 4> as.vector(blockrow)[1] 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 [23] 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 [45] 9 10 11 12 See ?row and ?col HTH, Marc Schwartz