Hi,I would like to reshape the following "wide" data set to "long" form. I would appreciate help with the correct code for "reshape". I tried a few unsuccessfully. Thanks. Chetty __________________________________________________ dat.1 Grp X0 X3 X6 X12 X25 X501 C 0.5326517 0.6930942 0.9403883 1.157571 2.483117 2.809012 2 C 0.4715917 0.8613200 2.0706117 2.937632 7.530960 7.120678 3 C 0.5021217 0.7772071 1.5055000 2.047601 5.007038 4.964845 4 E 0.5845200 0.6439033 0.8221400 2.148100 1.862153 12.412500 5 E 0.2985675 0.6561142 1.0987058 2.842180 4.057899 2.319930 6 E 1.7854500 1.9813878 1.8340700 3.507966 1.120367 2.603823 7 E 6.4370500 2.2302000 18.5271000 17.139600 22.978400 7.116638 8 E 2.2763969 1.3779013 5.5705040 6.409461 7.504704 6.113223 9 T 0.9649327 0.9825045 1.0099582 1.713536 4.652827 3.877736 10 T 2.0774727 2.4916545 4.0087455 8.183382 26.381091 26.279636 11 T 2.2527000 2.6311091 3.1711727 4.316418 9.255364 28.771473 12 T 1.3989636 1.7640273 2.5264636 4.712164 23.606545 32.030000 13 T 1.4211167 1.5128417 1.7613583 2.351858 8.411725 55.448000 14 T 1.5992167 2.2668167 2.5927667 2.947150 6.025300 24.984767 15 T 2.9468333 3.3383667 3.1090667 3.244675 3.037425 2.679587 16 T 1.0867725 1.7652583 2.0307008 5.201625 8.279975 14.693583 17 T 1.7185010 2.0940723 2.5262791 4.083851 11.206282 23.595598 -- Professor of Family Medicine Boston University Tel: 617-414-6221, Fax:617-414-3345 emails: chettyvk@gmail.com,vchetty@bu.edu [[alternative HTML version deleted]]
Rowe, Brian Lee Yung (Portfolio Analytics)
2009-Feb-25 22:45 UTC
[R] reshape from wide to long
You didn't really provide enough detail on how you wanted the data reshaped, but I'll take a guess:> reshape(dat.1, direction='long',varying=paste('X',c(0,3,6,12,25,50),sep=''), sep='') Grp time X id 1.0 C 0 0.5326517 1 2.0 C 0 0.4715917 2 3.0 C 0 0.5021217 3 4.0 E 0 0.5845200 4 5.0 E 0 0.2985675 5 6.0 E 0 1.7854500 6 7.0 E 0 6.4370500 7 ... HTH, Brian -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Veerappa Chetty Sent: Wednesday, February 25, 2009 5:00 PM To: r-help at r-project.org Subject: [R] reshape from wide to long Hi,I would like to reshape the following "wide" data set to "long" form. I would appreciate help with the correct code for "reshape". I tried a few unsuccessfully. Thanks. Chetty __________________________________________________ dat.1 Grp X0 X3 X6 X12 X25 X501 C 0.5326517 0.6930942 0.9403883 1.157571 2.483117 2.809012 2 C 0.4715917 0.8613200 2.0706117 2.937632 7.530960 7.120678 3 C 0.5021217 0.7772071 1.5055000 2.047601 5.007038 4.964845 4 E 0.5845200 0.6439033 0.8221400 2.148100 1.862153 12.412500 5 E 0.2985675 0.6561142 1.0987058 2.842180 4.057899 2.319930 6 E 1.7854500 1.9813878 1.8340700 3.507966 1.120367 2.603823 7 E 6.4370500 2.2302000 18.5271000 17.139600 22.978400 7.116638 8 E 2.2763969 1.3779013 5.5705040 6.409461 7.504704 6.113223 9 T 0.9649327 0.9825045 1.0099582 1.713536 4.652827 3.877736 10 T 2.0774727 2.4916545 4.0087455 8.183382 26.381091 26.279636 11 T 2.2527000 2.6311091 3.1711727 4.316418 9.255364 28.771473 12 T 1.3989636 1.7640273 2.5264636 4.712164 23.606545 32.030000 13 T 1.4211167 1.5128417 1.7613583 2.351858 8.411725 55.448000 14 T 1.5992167 2.2668167 2.5927667 2.947150 6.025300 24.984767 15 T 2.9468333 3.3383667 3.1090667 3.244675 3.037425 2.679587 16 T 1.0867725 1.7652583 2.0307008 5.201625 8.279975 14.693583 17 T 1.7185010 2.0940723 2.5262791 4.083851 11.206282 23.595598 -- Professor of Family Medicine Boston University Tel: 617-414-6221, Fax:617-414-3345 emails: chettyvk at gmail.com,vchetty at bu.edu [[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. -------------------------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to "Merrill Lynch" are references to any company in the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. --------------------------------------------------------------------------
You can use the 'reshape' package:> x <- read.table(textConnection(" Grp X0 X3 X6 X12 X25 X50+ 1 C 0.5326517 0.6930942 0.9403883 1.157571 2.483117 2.809012 + 2 C 0.4715917 0.8613200 2.0706117 2.937632 7.530960 7.120678 + 3 C 0.5021217 0.7772071 1.5055000 2.047601 5.007038 4.964845 + 4 E 0.5845200 0.6439033 0.8221400 2.148100 1.862153 12.412500 + 5 E 0.2985675 0.6561142 1.0987058 2.842180 4.057899 2.319930 + 6 E 1.7854500 1.9813878 1.8340700 3.507966 1.120367 2.603823 + 7 E 6.4370500 2.2302000 18.5271000 17.139600 22.978400 7.116638 + 8 E 2.2763969 1.3779013 5.5705040 6.409461 7.504704 6.113223 + 9 T 0.9649327 0.9825045 1.0099582 1.713536 4.652827 3.877736 + 10 T 2.0774727 2.4916545 4.0087455 8.183382 26.381091 26.279636 + 11 T 2.2527000 2.6311091 3.1711727 4.316418 9.255364 28.771473 + 12 T 1.3989636 1.7640273 2.5264636 4.712164 23.606545 32.030000 + 13 T 1.4211167 1.5128417 1.7613583 2.351858 8.411725 55.448000 + 14 T 1.5992167 2.2668167 2.5927667 2.947150 6.025300 24.984767 + 15 T 2.9468333 3.3383667 3.1090667 3.244675 3.037425 2.679587 + 16 T 1.0867725 1.7652583 2.0307008 5.201625 8.279975 14.693583 + 17 T 1.7185010 2.0940723 2.5262791 4.083851 11.206282 23.595598"), header=TRUE)> closeAllConnections() > require(reshape)Loading required package: reshape Loading required package: plyr> x.m <- melt(x)Using Grp as id variables> > head(x.m)Grp variable value 1 C X0 0.5326517 2 C X0 0.4715917 3 C X0 0.5021217 4 E X0 0.5845200 5 E X0 0.2985675 6 E X0 1.7854500>On Wed, Feb 25, 2009 at 4:59 PM, Veerappa Chetty <chettyvk at gmail.com> wrote:> Hi,I would like to reshape the following "wide" data set to "long" form. I > would appreciate help with the correct code for "reshape". I tried a few > unsuccessfully. > Thanks. > Chetty > __________________________________________________ > dat.1 > > ?Grp ? ? ? ?X0 ? ? ? ?X3 ? ? ? ? ? ? ? X6 ? ? ? ? ? X12 ? ? ? ? ?X25 > ? ?X501 ? ?C 0.5326517 0.6930942 ?0.9403883 ?1.157571 ?2.483117 ?2.809012 > 2 ? ?C 0.4715917 0.8613200 ?2.0706117 ?2.937632 ?7.530960 ?7.120678 > 3 ? ?C 0.5021217 0.7772071 ?1.5055000 ?2.047601 ?5.007038 ?4.964845 > 4 ? ?E 0.5845200 0.6439033 ?0.8221400 ?2.148100 ?1.862153 12.412500 > 5 ? ?E 0.2985675 0.6561142 ?1.0987058 ?2.842180 ?4.057899 ?2.319930 > 6 ? ?E 1.7854500 1.9813878 ?1.8340700 ?3.507966 ?1.120367 ?2.603823 > 7 ? ?E 6.4370500 2.2302000 18.5271000 17.139600 22.978400 ?7.116638 > 8 ? ?E 2.2763969 1.3779013 ?5.5705040 ?6.409461 ?7.504704 ?6.113223 > 9 ? ?T 0.9649327 0.9825045 ?1.0099582 ?1.713536 ?4.652827 ?3.877736 > 10 ? T 2.0774727 2.4916545 ?4.0087455 ?8.183382 26.381091 26.279636 > 11 ? T 2.2527000 2.6311091 ?3.1711727 ?4.316418 ?9.255364 28.771473 > 12 ? T 1.3989636 1.7640273 ?2.5264636 ?4.712164 23.606545 32.030000 > 13 ? T 1.4211167 1.5128417 ?1.7613583 ?2.351858 ?8.411725 55.448000 > 14 ? T 1.5992167 2.2668167 ?2.5927667 ?2.947150 ?6.025300 24.984767 > 15 ? T 2.9468333 3.3383667 ?3.1090667 ?3.244675 ?3.037425 ?2.679587 > 16 ? T 1.0867725 1.7652583 ?2.0307008 ?5.201625 ?8.279975 14.693583 > 17 ? T 1.7185010 2.0940723 ?2.5262791 ?4.083851 11.206282 23.595598 > > > -- > Professor of Family Medicine > Boston University > Tel: 617-414-6221, Fax:617-414-3345 > emails: chettyvk at gmail.com,vchetty at bu.edu > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?