[R] sort columns

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Feb 1 14:10:14 CET 2006


try something like the following:

data[, order(as.numeric(gsub("v", "", names(data))))]


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://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm



----- Original Message ----- 
From: "Ana Quitério" <ana.quiterio at ine.pt>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, February 01, 2006 12:55 PM
Subject: [R] sort columns


Hi.



I have a simple (I think) question



My dataset have these variables:



names(data)

  [1] "v1"   "v2"   "v3"   "v4"   "v5"   "v6"   "v7"   "v8"   "v9" 
"v10"
"v11"  "v12"  "v13"  "v14"  "v15"  "v16"  "v17"  "v18"  "v52"

 [20] "v53"  "v54"  "v55"  "v56"  "v57"  "v58"  "v59"  "v60"  "v61" 
"v62"
"v63"  "v64"  "v65"  "v66"  "v67"  "v68"  "v69"  "v70"  "v71"

 [39] "v72"  "v73"  "v74"  "v75"  "v76"  "v77"  "v78"  "v79"  "v80" 
"v81"
"v19"  "v20"  "v21"  "v22"  "v23"  "v24"  "v25"  "v26"  "v27"

 [58] "v28"  "v29"  "v30"  "v31"  "v32"  "v33"  "v34"  "v35"  "v36" 
"v37"
"v38"  "v39"  "v40"  "v41"  "v42"  "v43"  "v44"  "v45"  "v46"

 [77] "v47"  "v48"  "v49"  "v50"  "v51"  "v82"  "v83"  "v84"  "v85" 
"v86"
"v87"  "v88"  "v89"  "v90"  "v91"  "v92"  "v93"  "v94"  "v95"

 [96] "v96"  "v97"  "v98"  "v99"  "v100" "v101" "v102" "v103" "v104" 
"v105"
"v106" "v107" "v108" "v109" "v110" "v111" "v112" "v113" "v114"

(...)

 [856] "v856" "v857" "v858" "v859" "v860" "v861" "v862" "v863" "v864" 
"v865"
"v866" "v867" "v868" "v869" "v870" "v871" "v872" "v873" "v874"

[875] "v875" "v876" "v877" "v878" "v879" "v880" "v881" "v882" "v883" 
"v884"
"v885" "v886" "v887" "v888" "v889" "v890" "v891" "v892" "v893"

[894] "v894" "v895" "v896" "v897" "v898" "v899" "v900" "v901" "v902" 
"v903"
"v904" "v905" "v906" "v907" "v908" "v909" "v910" "v911" "v912"

[913] "v913" "v914" "v915" "v916" "v917" "v918" "v919" "v920" "v921" 
"v922"
"v923" "v924" "v925" "v926" "v927" "v928" "v929" "v930" "v931"



And I want obtain another dataset with sort columns names, and I do 
this:



data1<-data[,sort(colnames(data))]



names(data1)



[1] "v1"   "v10"  "v100" "v101" "v102" "v103" "v104" "v105" "v106" 
"v107"
"v108" "v109" "v11"  "v110" "v111" "v112" "v113" "v114" "v115"

 [20] "v116" "v117" "v118" "v119" "v12"  "v120" "v121" "v122" "v123" 
"v124"
"v125" "v126" "v127" "v128" "v129" "v13"  "v130" "v131" "v132"

 [39] "v133" "v134" "v135" "v136" "v137" "v138" "v139" "v14"  "v140" 
"v141"
"v142" "v143" "v144" "v145" "v146" "v147" "v148" "v149" "v15"

 [58] "v150" "v151" "v152" "v153" "v154" "v155" "v156" "v157" "v158" 
"v159"
"v16"  "v160" "v161" "v162" "v163" "v164" "v165" "v166" "v167"

 [77] "v168" "v169" "v17"  "v170" "v171" "v172" "v173" "v174" "v175" 
"v176"
"v177" "v178" "v179" "v18"  "v180" "v181" "v182" "v183" "v184"

 [96] "v185" "v186" "v187" "v188" "v189" "v19"  "v190" "v191" "v192" 
"v193"
"v194" "v195" "v196" "v197" "v198" "v199" "v2"   "v20"  "v200"

[115] "v201" "v202" "v203" "v204" "v205" "v206" "v207" "v208" "v209" 
"v21"
"v210" "v211" "v212" "v213" "v214" "v215" "v216" "v217" "v218"

[134] "v219" "v22"  "v220" "v221" "v222" "v223" "v224" "v225" "v226" 
"v227"
"v228" "v229" "v23"  "v230" "v231" "v232" "v233" "v234" "v235"

(...)

[856] "v87"  "v870" "v871" "v872" "v873" "v874" "v875" "v876" "v877" 
"v878"
"v879" "v88"  "v880" "v881" "v882" "v883" "v884" "v885" "v886"

[875] "v887" "v888" "v889" "v89"  "v890" "v891" "v892" "v893" "v894" 
"v895"
"v896" "v897" "v898" "v899" "v9"   "v90"  "v900" "v901" "v902"

[894] "v903" "v904" "v905" "v906" "v907" "v908" "v909" "v91"  "v910" 
"v911"
"v912" "v913" "v914" "v915" "v916" "v917" "v918" "v919" "v92"

[913] "v920" "v921" "v922" "v923" "v924" "v925" "v926" "v927" "v928" 
"v929"
"v93"  "v930" "v931" "v94"  "v95"  "v96"  "v97"  "v98"  "v99"





But I would like obtain, something like this: "v1"  "v2"  "v3" "v4" 
"v5"
"v6"  (...)   "v928"  "v929"  "v930" "v931"



It's possible?



Thanks in advance



Ana Quiterio



Ana Quiterio

INE - DME
Serviço de Metodos Estatisticos
Tel: +351 21 842 61 00 (Ext: 3222)
E-mail: ana.quiterio at ine.pt <mailto:ana.quiterio at ine.pt>

Lisbon/Portugal












[[alternative HTML version deleted]]




--------------------------------------------------------------------------------


> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list