An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/fcce7785/attachment.pl>
How to change the order of columns in a data frame?
10 messages · Joel Fürstenberg-Hägg, Alfredo Alessandrini, jim holtman +2 more
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
Sarah 2012/2/17 Joel F?rstenberg-H?gg <joelf at life.ku.dk>:
Dear all, I have a data frame in which the columns need to be ordered. The first column X is at the right position, but the remaining columns X1-Xn should be ordered like this: X1, X2, X3 etc instead of like below.
colnames(pos1)
?[1] "X" ? "X1" ?"X10" "X11" "X12" "X13" "X14" "X15" "X16" "X17" "X18" "X19" "X2" ?"X20" "X3" ?"X4" ?"X5" ?"X6" ?"X7" ?"X8" ?"X9"
pos1[1:5,1:5]
? ? ?X ? ? ? X1 ? ? ? X10 ? ? ? X11 ? ? ? X12 1 100.5 7949.469 18509.064 ?8484.969 17401.056 2 101.5 3080.058 ?7794.691 ?3211.323 ?8211.058 3 102.5 1854.347 ?4347.571 ?1783.846 ?4827.338 4 103.5 2064.441 ?8421.746 ?2012.536 ?8363.785 5 104.5 9650.402 26637.926 10730.647 27053.421 I am trying to first change the first column name to something without an X and save as a vector. I would then remove the X from each position use the vector for renaming the columns. Then the column 2-n could be ordered, I hope... colnames(pos)[1] <- "Mass" columnNames <- colnames(pos) Does any of you have an idea how to do this, or perhaps there is a smoother solution? Would it be easier to solve it if the contents of the first column were extracted and used as row names instead? Best regards, Joel
Sarah Goslee http://www.functionaldiversity.org
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/c4752bee/attachment.pl>
pos2 <- pos1[, c("X", "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8",
"X9", "X10", "X11", "X12",
"X13", "X14", "X15", "X16", "X17", "X18", "X19", "X20")]
2012/2/17 Joel F?rstenberg-H?gg <joelf at life.ku.dk>:
Dear all, I have a data frame in which the columns need to be ordered. The first column X is at the right position, but the remaining columns X1-Xn should be ordered like this: X1, X2, X3 etc instead of like below.
colnames(pos1)
?[1] "X" ? "X1" ?"X10" "X11" "X12" "X13" "X14" "X15" "X16" "X17" "X18" "X19" "X2" ?"X20" "X3" ?"X4" ?"X5" ?"X6" ?"X7" ?"X8" ?"X9"
pos1[1:5,1:5]
? ? ?X ? ? ? X1 ? ? ? X10 ? ? ? X11 ? ? ? X12 1 100.5 7949.469 18509.064 ?8484.969 17401.056 2 101.5 3080.058 ?7794.691 ?3211.323 ?8211.058 3 102.5 1854.347 ?4347.571 ?1783.846 ?4827.338 4 103.5 2064.441 ?8421.746 ?2012.536 ?8363.785 5 104.5 9650.402 26637.926 10730.647 27053.421 I am trying to first change the first column name to something without an X and save as a vector. I would then remove the X from each position use the vector for renaming the columns. Then the column 2-n could be ordered, I hope... colnames(pos)[1] <- "Mass" columnNames <- colnames(pos) Does any of you have an idea how to do this, or perhaps there is a smoother solution? Would it be easier to solve it if the contents of the first column were extracted and used as row names instead? Best regards, Joel ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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 Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Fri, Feb 17, 2012 at 02:26:52PM +0100, Joel F?rstenberg-H?gg wrote:
Dear all, I have a data frame in which the columns need to be ordered. The first column X is at the right position, but the remaining columns X1-Xn should be ordered like this: X1, X2, X3 etc instead of like below.
colnames(pos1)
[1] "X" "X1" "X10" "X11" "X12" "X13" "X14" "X15" "X16" "X17" "X18" "X19" "X2" "X20" "X3" "X4" "X5" "X6" "X7" "X8" "X9"
pos1[1:5,1:5]
X X1 X10 X11 X12 1 100.5 7949.469 18509.064 8484.969 17401.056 2 101.5 3080.058 7794.691 3211.323 8211.058 3 102.5 1854.347 4347.571 1783.846 4827.338 4 103.5 2064.441 8421.746 2012.536 8363.785 5 104.5 9650.402 26637.926 10730.647 27053.421 I am trying to first change the first column name to something without an X and save as a vector. I would then remove the X from each position use the vector for renaming the columns. Then the column 2-n could be ordered, I hope... colnames(pos)[1] <- "Mass" columnNames <- colnames(pos)
Hi.
Try the following. For simplicity, i assume the first
column to be X0, but this is not necessary. Example data
a <- sort(paste("A", 0:19, sep=""))
names(a) <- sort(paste("X", 0:19, sep=""))
pos <- data.frame(rbind(a))
pos
X0 X1 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X2 X3 X4 X5 X6 X7 X8 X9
a A0 A1 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19 A2 A3 A4 A5 A6 A7 A8 A9
The reordering:
columnNames <- colnames(pos)
ind <- as.integer(substr(columnNames, 2, nchar(columnNames)))
pos[, order(ind)]
X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19
a A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19
Hope this helps.
Petr Savicky.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/1861faea/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/0fcbe192/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/55d315c6/attachment.pl>
Sorry, it should be:
fakedata[, order(pos)]
A X1 X2 X3 X4 X5 X6 X7 X8 X9 1 0 1 2 3 4 5 6 7 8 9 2 0 1 2 3 4 5 6 7 8 9 3 0 1 2 3 4 5 6 7 8 9 Using order also ensures that non-sequential column ids will work:
fakedata <- data.frame(A=c(0,0,0), X1=c(1,1,1), X6=c(6,6,6), X7=c(7,7,7), X3=c(3,3,3), X4=c(4,4,4), X9=c(9,9,9), X2=c(2,2,2), X8=c(8,8,8))
pos <- colnames(fakedata)[2:ncol(fakedata)]
pos <- c(1, 1+as.numeric(gsub("X", "", pos)))
fakedata
A X1 X6 X7 X3 X4 X9 X2 X8 1 0 1 6 7 3 4 9 2 8 2 0 1 6 7 3 4 9 2 8 3 0 1 6 7 3 4 9 2 8
fakedata[, order(pos)]
A X1 X2 X3 X4 X6 X7 X8 X9 1 0 1 2 3 4 6 7 8 9 2 0 1 2 3 4 6 7 8 9 3 0 1 2 3 4 6 7 8 9 Sarah 2012/2/17 Joel F?rstenberg-H?gg <joelf at life.ku.dk>:
It does not work when using more variables, and my data frames usually contains about thousand columns... Best, Joel
fakedata <- data.frame(A=c(0,0,0), X1=c(1,1,1), X6=c(6,6,6), X7=c(7,7,7), X3=c(3,3,3), X4=c(4,4,4), X9=c(9,9,9), X2=c(2,2,2), X8=c(8,8,8), X5=c(5,5,5)) fakedata
? A X1 X6 X7 X3 X4 X9 X2 X8 X5 1 0? 1? 6? 7? 3? 4? 9? 2? 8? 5 2 0? 1? 6? 7? 3? 4? 9? 2? 8? 5 3 0? 1? 6? 7? 3? 4? 9? 2? 8? 5
pos <- colnames(fakedata)[2:ncol(fakedata)] pos
[1] "X1" "X6" "X7" "X3" "X4" "X9" "X2" "X8" "X5"
pos <- c(1, 1+as.numeric(gsub("X", "", pos)))
pos
?[1]? 1? 2? 7? 8? 4? 5 10? 3? 9? 6
fakedata[,? pos]
? A X1 X9 X2 X7 X3 X5 X6 X8 X4 1 0? 1? 9? 2? 7? 3? 5? 6? 8? 4 2 0? 1? 9? 2? 7? 3? 5? 6? 8? 4 3 0? 1? 9? 2? 7? 3? 5? 6? 8? 4
Sarah Goslee <sarah.goslee at gmail.com> 17-02-2012 14:36 >>>
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
Sarah 2012/2/17 Joel F?rstenberg-H?gg <joelf at life.ku.dk>:
Dear all, I have a data frame in which the columns need to be ordered. The first column X is at the right position, but the remaining columns X1-Xn should be ordered like this: X1, X2, X3 etc instead of like below.
colnames(pos1)
? [1] "X"?? "X1"? "X10" "X11" "X12" "X13" "X14" "X15" "X16" "X17" "X18" "X19" "X2"? "X20" "X3"? "X4"? "X5"? "X6"? "X7"? "X8"? "X9"
pos1[1:5,1:5]
????? X?????? X1?????? X10?????? X11?????? X12 1 100.5 7949.469 18509.064? 8484.969 17401.056 2 101.5 3080.058? 7794.691? 3211.323? 8211.058 3 102.5 1854.347? 4347.571? 1783.846? 4827.338 4 103.5 2064.441? 8421.746? 2012.536? 8363.785 5 104.5 9650.402 26637.926 10730.647 27053.421 I am trying to first change the first column name to something without an X and save as a vector. I would then remove the X from each position use the vector for renaming the columns. Then the column 2-n could be ordered, I hope... colnames(pos)[1] <- "Mass" columnNames <- colnames(pos) Does any of you have an idea how to do this, or perhaps there is a smoother solution? Would it be easier to solve it if the contents of the first column were extracted and used as row names instead? Best regards, Joel
-- Sarah Goslee http://www.functionaldiversity.org
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120217/b3eecd1f/attachment.pl>