Check out the reshape() function of the reshape package. Here's one of the examples from ?reshape.
Jean library(reshape) wide <- reshape(Indometh, v.names="conc", idvar="Subject", timevar="time", direction="wide") long <- reshape(wide, direction="long") wide long On Tue, Jul 23, 2013 at 9:35 AM, Andrea Lamont <alamont...@gmail.com> wrote: > Hello: > > I am running a simulation study and am stuck with a subsetting problem. > > Here is the basic issue: > I generated data and am running a simulation that uses multiple imputation. > For each generated dataset, I used multiple imputation. The resultant > dataset is in wide for where each imputation is recorded as a separate > column (though the different simulations are stacked). Here is an example > of what it looks like: > > sim X1 X2 X3 sim.1 X1.1 X1.1 X3.1 > 1 # # # # # # # > 1 # # # # # # # > 1 # # # # # # # > 2 # # # # # # # > 2 # # # # # # # > 2 # # # # # # # > > sim refers to the simulated/generated dataset. X1-X3 are the values for the > first imputed dataset, X1.1-X3.1 are the values for the second imputed > dataset. > > The problem is that I want the data to be in long format, like this: > > sim m X1 X2 X3 > 1 1 # # # > 1 2 # # # > 2 1 # # # > 2 2 # # # > > where m is the imputation number. > This will allow me to do cleaner calculations (e.g. X3-X1). > > I know I can subset the data manually - e.g. [,1:10] and save this to > separate datasets then rbind; however, I'm looking for a more flexible > approach to do this. This manual approach would be quite tedious as number > of imputations (and therefore number of columns) increased (with only 10 > imputations, there are roughly 810 columns). Also,I would like to > avoid having to recode each time I change the number of imputations. > > THe same is true for the reshape function, which would require naming > a huge number of columns and edits each time 'm' changes. > > > Is there a flexible way to approach this? I'm inclined to use a for loop, > but know that 1) this is generally inefficient and 2) am having trouble > with > the coding regardless. > > Any suggestions are appreciated. > > Thanks, > Andrea > > > -- > Andrea Lamont, MA > Clinical-Community Psychology > University of South Carolina > Barnwell College > Columbia, SC 29208 > > Please consider the environment before printing this email. > > CONFIDENTIAL: This transmission is intended for the use of the > individual(s) or entity to which it is addressed, and may contain > information that is privileged, confidential, and exempt from disclosure > under applicable law. Should the reader of this message not be the intended > recipient(s), you are hereby notified that any dissemination, distribution, > or copying of this communication is strictly prohibited. If you are not > the intended recipient, please contact the sender by reply email and > destroy/delete all copies of the original message. > > [[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]] ______________________________________________ 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.