On May 27, 2013, at 20:17 , Kristi Glover wrote: > Hi R-User, > I am wondering how I can rearrange columns in a table in R. I do have very > big data set (4500 columns). I have given an example of the data set. > >> dput(dat) > structure(list(preV1001A1b = c(0.59, 0.3, 0.78, 0.43), preV15A1b = c(0.57, > 0.62, 0.51, 0.95), preV2032A1b = c(0.4, 0.8, 0.24, 0.34), preV2035A1b = > c(0.95, > 0.67, 0.81, 0.8), preV59A1b = c(0.05, 0.57, 0.03, 0.5)), .Names = > c("preV1001A1b", > "preV15A1b", "preV2032A1b", "preV2035A1b", "preV59A1b"), class = > "data.frame", row.names = c(NA, > -4L)) > > I wanted to make like this >> dput(dat1) > structure(list(preV15A1b = c(0.57, 0.62, 0.51, 0.95), preV59A1b = c(0.05, > 0.57, 0.03, 0.5), preV1001A1b = c(0.59, 0.3, 0.78, 0.43), preV2032A1b = > c(0.4, > 0.8, 0.24, 0.34), preV2035A1b = c(0.95, 0.67, 0.81, 0.8)), .Names = > c("preV15A1b", > "preV59A1b", "preV1001A1b", "preV2032A1b", "preV2035A1b"), class = > "data.frame", row.names = c(NA, > -4L)) > Any suggestions. > KG
Is there a particular logic to that ordering? Otherwise, the obvious way is nm <- c("preV15A1b", "preV59A1b", "preV1001A1b", "preV2032A1b", "preV2035A1b") dat1 <- dat[nm] Or, maybe you are looking for something like this? o <- order(as.numeric(sub("preV([0-9]*)A1b", "\\1", names(dat)))) (dat1 <- dat[o]) -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd....@cbs.dk Priv: pda...@gmail.com ______________________________________________ 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.