[R] Data manipulation - multiplicate cases

jim holtman jholtman at gmail.com
Fri Mar 27 13:32:39 CET 2009


Is this what you are looking for:

> x
    X Y Z
1 123 3 1
2 234 3 1
3 345 4 2
4 456 3 2
> new.x <- x[rep(seq(nrow(x)), times=x$Y),]
> new.x
      X Y Z
1   123 3 1
1.1 123 3 1
1.2 123 3 1
2   234 3 1
2.1 234 3 1
2.2 234 3 1
3   345 4 2
3.1 345 4 2
3.2 345 4 2
3.3 345 4 2
4   456 3 2
4.1 456 3 2
4.2 456 3 2
> new.x$Z <- ave(new.x$Z, new.x$X, FUN=function(z) c(rep(1,z[1]), rep(0, length(z) - z[1])))
> new.x
      X Y Z
1   123 3 1
1.1 123 3 0
1.2 123 3 0
2   234 3 1
2.1 234 3 0
2.2 234 3 0
3   345 4 1
3.1 345 4 1
3.2 345 4 0
3.3 345 4 0
4   456 3 1
4.1 456 3 1
4.2 456 3 0
>


On Thu, Mar 26, 2009 at 4:26 PM, MarcioRibeiro <mestat at pop.com.br> wrote:
>
> Hi listers,
> I am trying to arrange my data and I didn't find any information how to do
> it!
> I have a data with 3 variables: X Y Z
> 1-I would like to multiplicate de information of X according to the number I
> have for my Y variable...
> 2-Then I want to identify with a dicotomic variable by the number according
> my variable Z from X...
> I can do the first part by...
> z<-rep(x,y)
> But I don't know how to set a dicotomic variable according to Z...
> Exemple...
> I have...
> X      Y    Z
> 123   3    1
> 234   3    1
> 345   4    2
> 456   3    2
> I want to get...
> X      Y    Z
> 123   3    1
> 123   3    0
> 123   3    0
> 234   3    1
> 234   3    0
> 234   3    0
> 345   4    1
> 345   4    1
> 345   4    0
> 345   4    0
> 456   3    1
> 456   3    1
> 456   3    0
>
> Thanks in advance...
> --
> View this message in context: http://www.nabble.com/Data-manipulation---multiplicate-cases-tp22730453p22730453.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list