[R] how to create a vector with different categories in a simple way?

Petr Pikal petr.pikal at precheza.cz
Tue Oct 24 08:56:00 CEST 2006


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 <jenny197806 at yahoo.se>
To:             	r-help at 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 a        1       1    1
>   2 b        1       2    1
>   3 c        1        3    1
>   4 a        1        4   1
>   5 b        1        1    2
>   6 c        1        2    2
>   7 b        1        3    2
>   8 c        1        4   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 f        2     3      2
>   24 d       2     4      2
> 
>   32 a      2     4     4  and so on
>   .    .       .      .      .
>   .    .       .      .      .
>   .    .      .       .      . 
>   768 f  48   4        4
> 
>   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 a        1       1    1      1            1
>   2 b        1       2    1      1            1
>   3 c        1        3    1     1            1
>   4 a        1        4   1      1            1
>   5 b        1        1    2     1            1
>   6 c        1        2    2     1           1
>   7 b        1        3    2     1           1
>   8 c        1        4    2      1           1
>   9 d        1       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 f        2     3      2      1         2
>   24 d       2     4      2      1         2
> 
>   32 a      2     4     4  and so on
>   .    .       .      .      .
>   .    .       .      .      .
>   .    .      .       .      . 
>   768 f  48   4        4     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 at 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.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list