Thompson, David (MNR)
2008-Feb-12 17:19 UTC
[R] Reorder data frame columns by negating list of names
Hello, I would like to reorder columns in a data frame by their names as demonstrated below: Take this data frame: > xxx <- data.frame(matrix(1:40, ncol=8)) > names(xxx) <- letters[1:8] > xxx a b c d e f g h 1 1 6 11 16 21 26 31 36 2 2 7 12 17 22 27 32 37 3 3 8 13 18 23 28 33 38 4 4 9 14 19 24 29 34 39 5 5 10 15 20 25 30 35 40 and reorder the columns like this: > xxx[,c( c('b', 'd', 'h'), c('a', 'c', 'e', 'f', 'g') )] b d h a c e f g 1 6 16 36 1 11 21 26 31 2 7 17 37 2 12 22 27 32 3 8 18 38 3 13 23 28 33 4 9 19 39 4 14 24 29 34 5 10 20 40 5 15 25 30 35 where I only have to name the columns that I'm interested in moving to the first few positions, something like: > xxx[,c( c('b', 'd', 'h'), -c('b', 'd', 'h') )] Error in -c("b", "d", "h") : invalid argument to unary operator Suggestions? and Thank you, DaveT. ************************************* Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources david.john.thompson at ontario.ca http://ofri.mnr.gov.on.ca
Dimitris Rizopoulos
2008-Feb-12 17:33 UTC
[R] Reorder data frame columns by negating list of names
one way is the following: xxx <- data.frame(matrix(1:40, ncol=8)) names(xxx) <- letters[1:8] ind <- c('b', 'd', 'h') nams <- names(xxx) xxx[c(ind, nams[!nams %in% ind])] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Thompson, David (MNR)" <David.John.Thompson at ontario.ca> To: <r-help at r-project.org> Sent: Tuesday, February 12, 2008 6:19 PM Subject: [R] Reorder data frame columns by negating list of names> Hello, > > I would like to reorder columns in a data frame by their names as > demonstrated below: > > Take this data frame: > > xxx <- data.frame(matrix(1:40, ncol=8)) > > names(xxx) <- letters[1:8] > > xxx > a b c d e f g h > 1 1 6 11 16 21 26 31 36 > 2 2 7 12 17 22 27 32 37 > 3 3 8 13 18 23 28 33 38 > 4 4 9 14 19 24 29 34 39 > 5 5 10 15 20 25 30 35 40 > > and reorder the columns like this: > > xxx[,c( c('b', 'd', 'h'), c('a', 'c', 'e', 'f', 'g') )] > b d h a c e f g > 1 6 16 36 1 11 21 26 31 > 2 7 17 37 2 12 22 27 32 > 3 8 18 38 3 13 23 28 33 > 4 9 19 39 4 14 24 29 34 > 5 10 20 40 5 15 25 30 35 > > where I only have to name the columns that I'm interested in moving > to > the first few positions, something like: > > xxx[,c( c('b', 'd', 'h'), -c('b', 'd', 'h') )] > Error in -c("b", "d", "h") : invalid argument to unary operator > > Suggestions? and Thank you, DaveT. > ************************************* > Silviculture Data Analyst > Ontario Forest Research Institute > Ontario Ministry of Natural Resources > david.john.thompson at ontario.ca > http://ofri.mnr.gov.on.ca > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
jim holtman
2008-Feb-12 17:41 UTC
[R] Reorder data frame columns by negating list of names
try this:> x <- read.table(textConnection(" a b c d e f g h+ 1 1 6 11 16 21 26 31 36 + 2 2 7 12 17 22 27 32 37 + 3 3 8 13 18 23 28 33 38 + 4 4 9 14 19 24 29 34 39 + 5 5 10 15 20 25 30 35 40"), header=TRUE)> # initial columns > init.cols <- c('b', 'd', 'h') > # now get the remaining > remaining <- setdiff(colnames(x), init.cols) > x[,c(init.cols, remaining)]b d h a c e f g 1 6 16 36 1 11 21 26 31 2 7 17 37 2 12 22 27 32 3 8 18 38 3 13 23 28 33 4 9 19 39 4 14 24 29 34 5 10 20 40 5 15 25 30 35>On Feb 12, 2008 12:19 PM, Thompson, David (MNR) <David.John.Thompson at ontario.ca> wrote:> Hello, > > I would like to reorder columns in a data frame by their names as > demonstrated below: > > Take this data frame: > > xxx <- data.frame(matrix(1:40, ncol=8)) > > names(xxx) <- letters[1:8] > > xxx > a b c d e f g h > 1 1 6 11 16 21 26 31 36 > 2 2 7 12 17 22 27 32 37 > 3 3 8 13 18 23 28 33 38 > 4 4 9 14 19 24 29 34 39 > 5 5 10 15 20 25 30 35 40 > > and reorder the columns like this: > > xxx[,c( c('b', 'd', 'h'), c('a', 'c', 'e', 'f', 'g') )] > b d h a c e f g > 1 6 16 36 1 11 21 26 31 > 2 7 17 37 2 12 22 27 32 > 3 8 18 38 3 13 23 28 33 > 4 9 19 39 4 14 24 29 34 > 5 10 20 40 5 15 25 30 35 > > where I only have to name the columns that I'm interested in moving to > the first few positions, something like: > > xxx[,c( c('b', 'd', 'h'), -c('b', 'd', 'h') )] > Error in -c("b", "d", "h") : invalid argument to unary operator > > Suggestions? and Thank you, DaveT. > ************************************* > Silviculture Data Analyst > Ontario Forest Research Institute > Ontario Ministry of Natural Resources > david.john.thompson at ontario.ca > http://ofri.mnr.gov.on.ca > > ______________________________________________ > 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 Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Barry Rowlingson
2008-Feb-12 17:44 UTC
[R] Reorder data frame columns by negating list of names
Thompson, David (MNR) wrote:> Hello, > > I would like to reorder columns in a data frame by their names as > demonstrated below: > > Take this data frame: > > xxx <- data.frame(matrix(1:40, ncol=8)) > > names(xxx) <- letters[1:8] > > xxx > a b c d e f g h > 1 1 6 11 16 21 26 31 36 > 2 2 7 12 17 22 27 32 37 > 3 3 8 13 18 23 28 33 38 > 4 4 9 14 19 24 29 34 39 > 5 5 10 15 20 25 30 35 40 > > and reorder the columns like this: > > xxx[,c( c('b', 'd', 'h'), c('a', 'c', 'e', 'f', 'g') )] > b d h a c e f g > 1 6 16 36 1 11 21 26 31 > 2 7 17 37 2 12 22 27 32 > 3 8 18 38 3 13 23 28 33 > 4 9 19 39 4 14 24 29 34 > 5 10 20 40 5 15 25 30 35 > > where I only have to name the columns that I'm interested in moving to > the first few positions, something like: > > xxx[,c( c('b', 'd', 'h'), -c('b', 'd', 'h') )] > Error in -c("b", "d", "h") : invalid argument to unary operator > > Suggestions? and Thank you, DaveT.With: > move [1] "b" "d" "h" you can do: > xxx[,c(match(move,names(xxx)),(1:dim(xxx)[2])[-match(move,names(xxx))])] b d h a c e f g 1 6 16 36 1 11 21 26 31 2 7 17 37 2 12 22 27 32 3 8 18 38 3 13 23 28 33 4 9 19 39 4 14 24 29 34 5 10 20 40 5 15 25 30 35 It basically uses match() to convert names to column numbers. You cant mix positive and negative indices so you need to take the matches you've got away from the 1:dim(xxx)[2] and that gives you the leftover column numbers. Don't forget to add a comment. Barry