@Alfredo
 
The X is removed, but the reordering does not work:
 
> colnames(df)[1] <- "Mass"
> columnNames <- colnames(df)
> colnames(df)
 [1] "Mass" "X1"   "X10"  "X11"  "X12"  "X13"  "X14"  "X15"  "X16" 
"X17"  "X18"  "X19"  "X2"   "X20"  "X3"   "X4"   "X5"   "X6"   "X7"  
"X8"   "X9"  
> 
> colnames(df) <- gsub("X","",colnames(df))
> colnames(df)
 [1] "Mass" "1"    "10"   "11"   "12"   "13"   "14"   "15"   "16"  
"17"   "18"   "19"   "2"    "20"   "3"    "4"    "5"    "6"    "7"   
"8"    "9"
>
> df <- df[,colnames(df)]
> colnames(df)
 [1] "Mass" "1"    "10"   "11"   "12"   "13"   "14"   "15"   "16"  
"17"   "18"   "19"   "2"    "20"   "3"    "4"    "5"    "6"    "7"   
"8"    "9" 
 
Best,
 
Joel
 
>>> Alfredo Alessandrini <caveneb...@gmail.com> 17-02-2012 14:40 >>>
Hi Joel,

to replace the colnames:

colnames(dataframe <- )gsub("X","",colnames(dataframe))

to order by colnames:

dataframe <- dataframe[,colnames(dataframe)]



Alfredo


2012/2/17 Joel Fürstenberg-Hägg <jo...@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@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.



        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to