zhenjiang xu
2011-Mar-15 01:06 UTC
[R] how to reshape the data.frame from long to wide in a specific order
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 [[alternative HTML version deleted]]
Dennis Murphy
2011-Mar-15 02:11 UTC
[R] how to reshape the data.frame from long to wide in a specific order
Hi: This is straightforward with the reshape package: library(reshape) origdata.long$condition <- factor(origdata.long$condition, levels = c('first', 'second', 'control')) cast(origdata.long, subject + sex ~ condition) Using measurement as value column. Use the value argument to cast to override this choice subject sex first second control 1 1 M 12.3 10.7 7.9 2 2 F 10.6 11.1 6.3 3 3 F 13.1 13.8 9.5 4 4 M 13.4 12.9 11.5 HTH, Dennis On Mon, Mar 14, 2011 at 6:06 PM, zhenjiang xu <zhenjiang.xu@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 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Henrique Dallazuanna
2011-Mar-15 02:24 UTC
[R] how to reshape the data.frame from long to wide in a specific order
Try this: reshape(origdata.long, direction = 'wide', timevar = 'condition', idvar c('subject', 'sex')) On Mon, Mar 14, 2011 at 10:06 PM, zhenjiang xu <zhenjiang.xu@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 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Gabor Grothendieck
2011-Mar-15 03:04 UTC
[R] how to reshape the data.frame from long to wide in a specific order
On Mon, Mar 14, 2011 at 9: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.You could reorder the columns afterwards: reshape(origdata.long, dir = "wide", idvar = c("subject", "sex"), timevar = "condition")[ c(1:2, 4:5, 3) ] -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Reasonably Related Threads
- how to convert a data.frame to a list of dist objects for individual differences MDS?
- Systematically biased count data regression model
- how to modify the tickment of x-axis
- how to calculate the average values of each row in a matrix
- datos climáticos cambio de formato