Dieter Vanderelst
2007-Nov-27 17:19 UTC
[R] rearrange data: one line per subject, one column per condition
Dear R-list, Is there a way to convert the typical long R data-format to a 1-line per subject format? I have data formatted as: Group subj condition variable 1 1 1 746.36625 2 2 1 1076.152857 1 3 1 1076.152857 2 4 1 657.4263636 1 5 1 854.1266667 2 6 1 1191.676154 1 7 1 1028.175385 1 1 2 46.36625 2 2 2 76.152857 1 3 2 76.152857 2 4 2 57.4263636 1 5 2 54.1266667 2 6 2 191.676154 1 7 2 028.175385 ... Here, one line equals the value of one subjects VARIABLE in function of the GROUP and the CONDITION. However, I would like to rearrange the data so that the columns of my data equal the 2 conditions and the lines the subjects. This is something like: subj group condition1 condition2 1 1 746.36625 46.36625 2 2 1076.152857 76.152857 ... I know its possible the other way around. But that's not what I need (this time). Before anyone asks: Yes, I want to do some analysis on my data in SPSS, so I need the rearranged format. Regards and Thanks, Dieter ------------------------------------------ Dieter Vanderelst dieter _ vanderelst AT emailengine DOT org d DOT vanderelst AT tue DOT nl Eindhoven University of Technology Faculty of Industrial Design Designed Intelligence Group Den Dolech 2 5612 AZ Eindhoven The Netherlands Tel +31 40 247 91 11
Henrique Dallazuanna
2007-Nov-27 20:01 UTC
[R] rearrange data: one line per subject, one column per condition
Hi, reshape(df, direction="wide", idvar="subj", timevar="condition")[,c(2,1,3,5)] On 27/11/2007, Dieter Vanderelst <dieter_vanderelst@emailengine.org> wrote:> > Dear R-list, > > Is there a way to convert the typical long R data-format to a 1-line per > subject format? > > I have data formatted as: > > Group subj condition variable > 1 1 1 746.36625 > 2 2 1 1076.152857 > 1 3 1 1076.152857 > 2 4 1 657.4263636 > 1 5 1 854.1266667 > 2 6 1 1191.676154 > 1 7 1 1028.175385 > 1 1 2 46.36625 > 2 2 2 76.152857 > 1 3 2 76.152857 > 2 4 2 57.4263636 > 1 5 2 54.1266667 > 2 6 2 191.676154 > 1 7 2 028.175385 > ... > > Here, one line equals the value of one subjects VARIABLE in function of > the GROUP and the CONDITION. > > However, I would like to rearrange the data so that the columns of my data > equal the 2 conditions and the lines the subjects. This is something like: > > subj group condition1 condition2 > 1 1 746.36625 46.36625 > 2 2 1076.152857 76.152857 > ... > > I know its possible the other way around. But that's not what I need (this > time). > > Before anyone asks: Yes, I want to do some analysis on my data in SPSS, so > I need the rearranged format. > > Regards and Thanks, > Dieter > ------------------------------------------ > Dieter Vanderelst > > dieter _ vanderelst AT emailengine DOT org > d DOT vanderelst AT tue DOT nl > > Eindhoven University of Technology > Faculty of Industrial Design > Designed Intelligence Group > Den Dolech 2 > 5612 AZ Eindhoven > The Netherlands > Tel +31 40 247 91 11 > > ______________________________________________ > 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]]
jim holtman
2007-Nov-27 20:12 UTC
[R] rearrange data: one line per subject, one column per condition
You can use the reshape package:> > x <- read.table(textConnection("Group subj condition variable+ 1 1 1 746.36625 + 2 2 1 1076.152857 + 1 3 1 1076.152857 + 2 4 1 657.4263636 + 1 5 1 854.1266667 + 2 6 1 1191.676154 + 1 7 1 1028.175385 + 1 1 2 46.36625 + 2 2 2 76.152857 + 1 3 2 76.152857 + 2 4 2 57.4263636 + 1 5 2 54.1266667 + 2 6 2 191.676154 + 1 7 2 028.175385"), header=TRUE)> xGroup subj condition variable 1 1 1 1 746.36625 2 2 2 1 1076.15286 3 1 3 1 1076.15286 4 2 4 1 657.42636 5 1 5 1 854.12667 6 2 6 1 1191.67615 7 1 7 1 1028.17539 8 1 1 2 46.36625 9 2 2 2 76.15286 10 1 3 2 76.15286 11 2 4 2 57.42636 12 1 5 2 54.12667 13 2 6 2 191.67615 14 1 7 2 28.17538> require(reshape) > x.m <- melt(x, measure.var="variable") > cast(x.m, Group + subj ~ condition)Group subj 1 2 1 1 1 746.3663 46.36625 2 1 3 1076.1529 76.15286 3 1 5 854.1267 54.12667 4 1 7 1028.1754 28.17538 5 2 2 1076.1529 76.15286 6 2 4 657.4264 57.42636 7 2 6 1191.6762 191.67615>On Nov 27, 2007 12:19 PM, Dieter Vanderelst <dieter_vanderelst at emailengine.org> wrote:> Dear R-list, > > Is there a way to convert the typical long R data-format to a 1-line per subject format? > > I have data formatted as: > > Group subj condition variable > 1 1 1 746.36625 > 2 2 1 1076.152857 > 1 3 1 1076.152857 > 2 4 1 657.4263636 > 1 5 1 854.1266667 > 2 6 1 1191.676154 > 1 7 1 1028.175385 > 1 1 2 46.36625 > 2 2 2 76.152857 > 1 3 2 76.152857 > 2 4 2 57.4263636 > 1 5 2 54.1266667 > 2 6 2 191.676154 > 1 7 2 028.175385 > ... > > Here, one line equals the value of one subjects VARIABLE in function of the GROUP and the CONDITION. > > However, I would like to rearrange the data so that the columns of my data equal the 2 conditions and the lines the subjects. This is something like: > > subj group condition1 condition2 > 1 1 746.36625 46.36625 > 2 2 1076.152857 76.152857 > ... > > I know its possible the other way around. But that's not what I need (this time). > > Before anyone asks: Yes, I want to do some analysis on my data in SPSS, so I need the rearranged format. > > Regards and Thanks, > Dieter > ------------------------------------------ > Dieter Vanderelst > > dieter _ vanderelst AT emailengine DOT org > d DOT vanderelst AT tue DOT nl > > Eindhoven University of Technology > Faculty of Industrial Design > Designed Intelligence Group > Den Dolech 2 > 5612 AZ Eindhoven > The Netherlands > Tel +31 40 247 91 11 > > ______________________________________________ > 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 you are trying to solve?