[R] repeating categorical variable codes

Petr PIKAL petr.pikal at precheza.cz
Wed Oct 5 16:05:55 CEST 2011


> 
> Hi Petr,
> 
> Thank you for the reply. Unfortunately my repetition is not uniform, but 

> dependent on values given in column B, which varies by each row.
> 
> Does this make it any clearer?

Not much. 

let say

> A <- letters[1:10]
> B <- sample(1:3, 10, replace =TRUE)
> A
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> B
 [1] 2 3 2 3 1 1 1 3 1 1

Than 

> rep(A,B)
 [1] "a" "a" "b" "b" "b" "c" "c" "d" "d" "d" "e" "f" "g" "h" "h" "h" "i" 
"j"

gives you repeating letters according to values in second vector. If this 
is not what you want, try to send some artificial example which 
illustrates what you really have and what is desired result.

Regards
Petr

> 
> Ric

> On 5 October 2011 23:44, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> Hi
> 
> >
> > I would appreciate help in knowing how to repeat categorical variable
> code
> > given in column=A, by the number in a matching column=B.
> > For example, I have a categorical variable code attributed to a
> household=A
> > and want to replicate the code for all member of the household, as 
given
> in
> > column=B. I would like to have one sequence of categorical variable
> codes
> > for individuals in column C. I have ~9000 values in A and my C will be
> > ~52000.
> >
> > E.g
> > (A)    (B)      (C)
> > 1       1         1
> > 2       2         2
> > 1       1         2
> > 2       3         1
> >                     2
> >                     2
> >                     2
> >
> > Any ideas would be gratefully accepted by a novice R user.

> I am not sure if I understand your problem but does rep(A, B)
> 
> give you what you want?
> 
> rep(letters[1:3], 1:3)
> [1] "a" "b" "b" "c" "c" "c"
> 
> Regards
> Petr
> 
> 
> >
> > Ric
> >
> >    [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org 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.

> 
>



More information about the R-help mailing list