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
>
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',
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
Cathol
3 matches
Mail list logo