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.
Is this what you are looking for:> xX 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.xX 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.xX 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?
Bill.Venables at csiro.au
2009-Mar-28 02:36 UTC
[R] Data manipulation - multiplicate cases
Here is a suggestion. Let your data frame be 'dat':> datX Y Z 123 3 1 234 3 1 345 4 2 456 3 2 Try this: bigData <- data.frame(with(dat, rbind(cbind(X = rep(X, Z), Y = rep(Y, Y), Z = 1), cbind(X = rep(X, Y-Z), Y = rep(Y, Y-Z), Z = 0)))) bigData <- with(bigData, bigData[order(X, -Y), ]) Bill Venables. ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of MarcioRibeiro [mestat at pop.com.br] Sent: 27 March 2009 06:26 To: r-help at r-project.org Subject: [R] Data manipulation - multiplicate cases 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.