Re: [R] how to create a vector with different categories in a simple way?
Hi If you just want a specific sequences added to your data and you have your data ordered as shown why not to use simply blockrow - rep(1:12, each=64) blockcol - rep(rep(1:4, each=16), 12) HTH Petr On 23 Oct 2006 at 23:51, Jenny persson wrote: Date sent: Mon, 23 Oct 2006 23:51:02 +0200 (CEST) From: Jenny persson [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:Re: [R] how to create a vector with different categories in a simple way? Thank you so much, Marc and Phil. Unfortunenately, I misunderstood the problem myself and wasn't clear how i wanted the variables to be. I will describe the issue again and hope you can help me out. Here is part of data called layout Id Name block col row 1 a1 11 2 b1 21 3 c131 4 a14 1 5 b112 6 c122 7 b132 8 c14 2 9 d 1 1 3 10 e 1 2 3 11 a 1 3 3 12 d 1 4 3 13 e 1 1 4 14 a 1 2 4 15 d 1 3 4 16 c 1 4 4 17 d 2 1 1 18 c 2 2 1 19 e 2 3 1 20 d 2 4 1 21 b 2 1 2 22 e 2 2 2 23 f2 3 2 24 d 2 4 2 32 a 2 4 4 and so on .. . . . .. . . . .. . . . 768 f 48 44 As you can see for each row there are 4 columns. the total observations in each block is 16. My real data contains 48 blocks which give totally 768 observations. The blocks ranged from 1-48 are displayed four and four after each other like below. Note there are 4 rows and 4 columns in each block. 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 What I want is to create two variables called blockrow respective 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. The data should look like Id Name block col row blockrow blockcol 1 a1 11 11 2 b1 21 11 3 c131 11 4 a14 1 11 5 b112 11 6 c122 1 1 7 b132 1 1 8 c142 1 1 9 d1 1 3 1 1 10 e 1 2 3 1 1 11 a 1 3 3 1 1 12 d 1 4 3 1 1 13 e 1 1 4 1 1 14 a 1 2 4 1 1 15 d 1 3 4 1 1 16 c 1 4 4 1 1 17 d 2 1 1 1 2 18 c 2 2 1 1 2 19 e 2 3 1 1 2 20 d 2 4 1 1 2 21 b 2 1 2 1 2 22 e 2 2 2 1 2 23 f2 3 2 1 2 24 d 2 4 2 1 2 32 a 2 4 4 and so on .. . . . .. . . . .. . . . 768 f 48 44 12 4 I have an algorithm blockrow -1 if(layout$block = 4) blockrow -1 if(5=layout$block = 8) blockrow -2 if(9=layout$block = 12) blockrow -3 and so on Can I do a for loop like : #-- Append some more columns to matrix layout-- blockrow-rep(0,nrow(layout)) blockcol-rep(0,nrow(layout)) for (a in 1:12){ if(4*a+1=layout$block=(a+1)*4) blockrow-(a+1) } Similarly, blockcol-1 if(layout$block = 5,9,13,17,21,25,29,33,37,41,45) blockcol-1 if(layout$block = 2,6,10,14,18,22,26,30,34,38,42,46) blockcol-2 and so on which give the for loop blockcol-1 for (a in 1:12){ if(layout$block==(4*a+1)) blockrow-1 } or how can i do it in R so I get blockrow and blockcol as i want ? Thanks again for your help, Best regards, Yen [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
Re: [R] how to create a vector with different categories in a simple way?
Or as factors: blockrowfac - gl(12, 4 * 16) blockcolfac - gl(4, 16, 4 * 16 * 12) On 10/24/06, Petr Pikal [EMAIL PROTECTED] wrote: Hi If you just want a specific sequences added to your data and you have your data ordered as shown why not to use simply blockrow - rep(1:12, each=64) blockcol - rep(rep(1:4, each=16), 12) HTH Petr On 23 Oct 2006 at 23:51, Jenny persson wrote: Date sent: Mon, 23 Oct 2006 23:51:02 +0200 (CEST) From: Jenny persson [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:Re: [R] how to create a vector with different categories in a simple way? Thank you so much, Marc and Phil. Unfortunenately, I misunderstood the problem myself and wasn't clear how i wanted the variables to be. I will describe the issue again and hope you can help me out. Here is part of data called layout Id Name block col row 1 a1 11 2 b1 21 3 c131 4 a14 1 5 b112 6 c122 7 b132 8 c14 2 9 d 1 1 3 10 e 1 2 3 11 a 1 3 3 12 d 1 4 3 13 e 1 1 4 14 a 1 2 4 15 d 1 3 4 16 c 1 4 4 17 d 2 1 1 18 c 2 2 1 19 e 2 3 1 20 d 2 4 1 21 b 2 1 2 22 e 2 2 2 23 f2 3 2 24 d 2 4 2 32 a 2 4 4 and so on .. . . . .. . . . .. . . . 768 f 48 44 As you can see for each row there are 4 columns. the total observations in each block is 16. My real data contains 48 blocks which give totally 768 observations. The blocks ranged from 1-48 are displayed four and four after each other like below. Note there are 4 rows and 4 columns in each block. 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 What I want is to create two variables called blockrow respective 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. The data should look like Id Name block col row blockrow blockcol 1 a1 11 11 2 b1 21 11 3 c131 11 4 a14 1 11 5 b112 11 6 c122 1 1 7 b132 1 1 8 c142 1 1 9 d1 1 3 1 1 10 e 1 2 3 1 1 11 a 1 3 3 1 1 12 d 1 4 3 1 1 13 e 1 1 4 1 1 14 a 1 2 4 1 1 15 d 1 3 4 1 1 16 c 1 4 4 1 1 17 d 2 1 1 1 2 18 c 2 2 1 1 2 19 e 2 3 1 1 2 20 d 2 4 1 1 2 21 b 2 1 2 1 2 22 e 2 2 2 1 2 23 f2 3 2 1 2 24 d 2 4 2 1 2 32 a 2 4 4 and so on .. . . . .. . . . .. . . . 768 f 48 44 12 4 I have an algorithm blockrow -1 if(layout$block = 4) blockrow -1 if(5=layout$block = 8) blockrow -2 if(9=layout$block = 12) blockrow -3 and so on Can I do a for loop like : #-- Append some more columns to matrix layout-- blockrow-rep(0,nrow(layout)) blockcol-rep(0,nrow(layout)) for (a in 1:12){ if(4*a+1=layout$block=(a+1)*4) blockrow-(a+1) } Similarly, blockcol-1 if(layout$block = 5,9,13,17,21,25,29,33,37,41,45) blockcol-1 if(layout$block = 2,6,10,14,18,22,26,30,34,38,42,46) blockcol-2 and so on which give the for loop blockcol-1 for (a in 1:12){ if(layout$block==(4*a+1)) blockrow-1 } or how can i do it in R so I get blockrow and blockcol as i want ? Thanks again for your help, Best
Re: [R] how to create a vector with different categories in a simple way?
Thank you so much, Marc and Phil. Unfortunenately, I misunderstood the problem myself and wasn't clear how i wanted the variables to be. I will describe the issue again and hope you can help me out. Here is part of data called layout Id Name block col row 1 a1 11 2 b1 21 3 c131 4 a14 1 5 b112 6 c122 7 b132 8 c14 2 9 d 1 1 3 10 e 1 2 3 11 a 1 3 3 12 d 1 4 3 13 e 1 1 4 14 a 1 2 4 15 d 1 3 4 16 c 1 4 4 17 d 2 1 1 18 c 2 2 1 19 e 2 3 1 20 d 2 4 1 21 b 2 1 2 22 e 2 2 2 23 f2 3 2 24 d 2 4 2 32 a 2 4 4 and so on .. . . . .. . . . .. . . . 768 f 48 44 As you can see for each row there are 4 columns. the total observations in each block is 16. My real data contains 48 blocks which give totally 768 observations. The blocks ranged from 1-48 are displayed four and four after each other like below. Note there are 4 rows and 4 columns in each block. 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 What I want is to create two variables called blockrow respective 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. The data should look like Id Name block col row blockrow blockcol 1 a1 11 11 2 b1 21 11 3 c131 11 4 a14 1 11 5 b112 11 6 c122 1 1 7 b132 1 1 8 c142 1 1 9 d1 1 3 1 1 10 e 1 2 3 1 1 11 a 1 3 3 1 1 12 d 1 4 3 1 1 13 e 1 1 4 1 1 14 a 1 2 4 1 1 15 d 1 3 4 1 1 16 c 1 4 4 1 1 17 d 2 1 1 1 2 18 c 2 2 1 1 2 19 e 2 3 1 1 2 20 d 2 4 1 1 2 21 b 2 1 2 1 2 22 e 2 2 2 1 2 23 f2 3 2 1 2 24 d 2 4 2 1 2 32 a 2 4 4 and so on .. . . . .. . . . .. . . . 768 f 48 44 12 4 I have an algorithm blockrow -1 if(layout$block = 4) blockrow -1 if(5=layout$block = 8) blockrow -2 if(9=layout$block = 12) blockrow -3 and so on Can I do a for loop like : #-- Append some more columns to matrix layout-- blockrow-rep(0,nrow(layout)) blockcol-rep(0,nrow(layout)) for (a in 1:12){ if(4*a+1=layout$block=(a+1)*4) blockrow-(a+1) } Similarly, blockcol-1 if(layout$block = 5,9,13,17,21,25,29,33,37,41,45) blockcol-1 if(layout$block = 2,6,10,14,18,22,26,30,34,38,42,46) blockcol-2 and so on which give the for loop blockcol-1 for (a in 1:12){ if(layout$block==(4*a+1)) blockrow-1 } or how can i do it in R so I get blockrow and blockcol as i want ? Thanks again for your help, Best regards, Yen [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [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,]1234 [2,]5678 [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,]1111 [2,]2222 [3,]3333 [4,]4444 [5,]5555 [6,]6666 [7,]7777 [8,]8888 [9,]9999 [10,] 10 10 10 10 [11,] 11 11 11 11 [12,] 12 12 12 12 blockcol [,1] [,2] [,3] [,4] [1,]1234 [2,]1234 [3,]1234 [4,]1234 [5,]1234 [6,]1234 [7,]1234 [8,]1234 [9,]1234 [10,]1234 [11,]1234 [12,]1234 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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.