Stephen - Thanks a bunch for the suggestion to use "reshape". Thank is exactly what I needed. Here is the test code that I put together so far. I can probably get rid of the "time" and "id" columns from the resulting reshaped_test_data data.frame, but this is will allow me to move forward.
test_data1_df<-data.frame(Variables=c("Stall","Stall","Stall","Stall","Stall"), Run.Age=c(10, 20, 30, 40, 50), Run.1=c(1,2,3,4,5), Run.2=c(10,20,30,40,50), Run.3=c(11,21,31,41,51), Location=c("HSV", "ATH","HSV", "ATH","FLO")) test_data2_df<-data.frame(Variables=c("Stall","Stall","Stall","Stall","Stall", "Stall","Stall","Stall","Stall","Stall", "Stall","Stall","Stall","Stall","Stall"), Run.Age=c(10, 20, 30, 40, 50, 15, 25, 35, 45, 55, 18, 28, 38, 48, 58), Run.1=c(1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 11, 21, 31, 41, 51), Location=c("HSV", "ATH", "HSV", "ATH", "FLO", "HSV", "ATH", "HSV", "ATH", "FLO", "HSV", "ATH", "HSV", "ATH", "FLO")) test_data_df<-test_data1_df length_test_data<-length(grep('^Run',names(test_data_df))) if(length_test_data==2) { reshaped_test_data<-reshape(test_data_df, varying=list(c('Run.1')), idvar='Location',direction='long') } else if (length_test_data==4){ reshaped_test_data<-reshape(test_data_df, varying=list(c('Run.1','Run.2','Run.3')), #idvar=c('Location','Run.Age'), direction='long') direction='long') } Thanks also to Phil Spector who also provided similar advice. --- On Mon, 3/9/09, stephen sefick <ssef...@gmail.com> wrote: From: stephen sefick <ssef...@gmail.com> Subject: Re: [R] Data Restructuring Question To: jasonkrup...@yahoo.com Cc: R-help@r-project.org Date: Monday, March 9, 2009, 9:00 PM look at package reshape there is a cool little function input that once you get the hang of is handy. On Mon, Mar 9, 2009 at 5:50 PM, Jason Rupert <jasonkrup...@yahoo.com> wrote: > I think I am overlooking a call or concept in R to help me easily and quickly restructure my data.frame: > > Sometimes the data I receive looks like: > VariableName, Run1, Run2, Run3, Location > temp, 15.0, 16.0, 17.0, There > > And other times it looks like: > VariableName, Run, Location > temp, 17.0, There > > I would like to use the header information in order to be able to restructure the first data set to have a similar look as the second, i.e. > > VariableName, Run, Location, > temp, 15.0, There # Really Run1 > temp, 16.0, There # Really Run2 > temp, 17.0, There # Really Run3 > > Right now I am manually recombining: > tmp_1<-data.frame(data$VariableName, data$ Run1, data$Location) > tmp_2<-data.frame(data$VariableName, data$ Run2, data$Location) > tmp_3<-data.frame(data$VariableName, data$ Run3, data$Location) > > combine_1<-rbind(tmp_1, tmp_2) > combine_1<-rbind(combine_1, tmp_3) > > Is there an easier way that is more flexible? I would like to make it flexible enough to handle the case where I have two or four runs. > > Thank you for any feedback. > > > > [[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. > > -- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis [[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.