Re: [R] how to reshape the data.frame from long to wide in a specific order

From: Jeff Newmiller <jdnewmil_at_dcn.davis.ca.us>
Date: Mon, 14 Mar 2011 20:03:01 -0700

I just fix it afterward:

dta <- dta[,c("col1","col2","col3")]



Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil_at_dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k

Sent from my phone. Please excuse my brevity.

David Winsemius <dwinsemius_at_comcast.net> wrote:

On Mar 14, 2011, at 10:24 PM, Henrique Dallazuanna wrote: > Try this: > > reshape(origdata.long, direction = 'wide', timevar = 'condition', > idvar = > c('subject', 'sex')) If you first set the order in the desired sequence: origdata.long$condition <- factor(origdata.long$condition, levels=c('first', 'second', 'control') ) And then sort in that order before the reshape: reshape(origdata.long[order(origdata.long$condition), ], direction = 'wide', timevar = 'condition', idvar = c('subject', 'sex')) You get what you asked for. It seems to be driven by the order in which the values appear in the timevar and that_ might_ even be in the help page, but reshape has is one of the most confusing help pages on the planet. -- David. > > On Mon, Mar 14, 2011 at 10:06 PM, zhenjiang xu > <zhenjiang.xu_at_gmail.com>wrote: > >> Hi, >> >> For example, the data.frame like: >> >> origdata.long <- read.table(header=T, con <- textConnection(' >> subject sex condition measurement >> 1 M control 7.9 >>   1 M

first 12.3 >> 1 M second 10.7 >> 2 F control 6.3 >> 2 F first 10.6 >> 2 F second 11.1 >> 3 F control 9.5 >> 3 F first 13.1 >> 3 F second 13.8 >> 4 M control 11.5 >> 4 M first 13.4 >> 4 M second 12.9 >> ')) >> close(con) >> >> Given a vector c('first', 'second', 'control), how can I reshape the >> data.frame to this? >> # subject sex first second control >> # 1 M 12.3 10.7 7.9 >> # 2 F 10.6 11.1 6.3 >> # 3 F 13.1 13.8 9.5 >> # 4 M 13.4 12.9 11.5 >> >> I know reshape() can transform the data.frame from long to wide, >> but it >> seems not able to control the order of the columns. >> >> Thanks ahead of time >> -- >> Best, >> Zhenjiang David Winsemius, MD West Hartford, CT_____________________________________________
R-help@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.

        [[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. Received on Tue 15 Mar 2011 - 03:06:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Mar 2011 - 03:50:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive