Created
February 20, 2012 22:56
-
-
Save pedroj/1872102 to your computer and use it in GitHub Desktop.
Order columns (stack) in a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Order columns in a table | |
| fakedata <- data.frame(A=c(0,0,0), X2=c(2,2,2), X1=c(1,1,1), X3=c(3,3,3)) | |
| fakedata | |
| A X2 X1 X3 | |
| 1 0 2 1 3 | |
| 2 0 2 1 3 | |
| 3 0 2 1 3 | |
| pos <- colnames(fakedata)[2:ncol(fakedata)] | |
| pos <- c(1, 1+as.numeric(gsub("X", "", pos))) | |
| fakedata[, pos] | |
| A X1 X2 X3 | |
| 1 0 1 2 3 | |
| 2 0 1 2 3 | |
| 3 0 1 2 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment