使用R更改数据框中列的顺序

时间:2013-04-16 21:50:53

标签: r

我的数据如下:

nnn <- structure(list(JulDay = c(260.0208435, 260.0416565, 260.0625, 
260.0833435, 260.1041565, 260.125), i46j8k1 = c(17.99964905, 
18.5903511, 22.93223381, 25.75687981, 25.78559113, 25.8169651
), i47j8k1 = c(18.36571884, 21.15985107, 24.80698013, 25.75730324, 
25.78366089, 25.65381622), i48j8k1 = c(18.45543289, 22.02331543, 
24.99463654, 25.5712738, 25.64232635, 25.66119385), i46j8k2 = c(17.99798965, 
18.60897827, 22.95389748, 25.75719261, 25.78653336, 25.81692505
), i47j8k2 = c(18.36762619, 21.17233467, 24.82248497, 25.75767899, 
25.7840023, 25.64115906), i48j8k2 = c(18.45938683, 22.04619026, 
24.9859066, 25.56440544, 25.63998032, 25.66089439), i46j8k3 = c(17.99430084, 
17.92090797, 19.67384911, 21.70389938, 23.67287827, 24.04380417
), i47j8k3 = c(18.36631203, 19.82550049, 21.42166328, 23.76496887, 
24.87460899, 25.41026688), i48j8k3 = c(18.44684792, 20.60981369, 
22.68632317, 24.1877079, 25.12503052, 24.99880028), i46j8k4 = c(17.98368073, 
17.92047691, 18.11532974, 18.67082596, 20.23210907, 21.7181263
), i47j8k4 = c(18.33089066, 18.41581535, 18.53464127, 19.18972206, 
20.14752388, 20.11002922), i48j8k4 = c(18.21522522, 18.52231598, 
19.21397209, 19.58755302, 20.11982536, 21.28104591)), .Names = c("JulDay", 
"i46j8k1", "i47j8k1", "i48j8k1", "i46j8k2", "i47j8k2", "i48j8k2", 
"i46j8k3", "i47j8k3", "i48j8k3", "i46j8k4", "i47j8k4", "i48j8k4"
), row.names = c(NA, 6L), class = "data.frame")

> nnn
    JulDay  i46j8k1  i47j8k1  i48j8k1  i46j8k2  i47j8k2  i48j8k2  i46j8k3  i47j8k3  i48j8k3  i46j8k4  i47j8k4  i48j8k4
1 260.0208 17.99965 18.36572 18.45543 17.99799 18.36763 18.45939 17.99430 18.36631 18.44685 17.98368 18.33089 18.21523
2 260.0417 18.59035 21.15985 22.02332 18.60898 21.17233 22.04619 17.92091 19.82550 20.60981 17.92048 18.41582 18.52232
3 260.0625 22.93223 24.80698 24.99464 22.95390 24.82248 24.98591 19.67385 21.42166 22.68632 18.11533 18.53464 19.21397
4 260.0833 25.75688 25.75730 25.57127 25.75719 25.75768 25.56441 21.70390 23.76497 24.18771 18.67083 19.18972 19.58755
5 260.1042 25.78559 25.78366 25.64233 25.78653 25.78400 25.63998 23.67288 24.87461 25.12503 20.23211 20.14752 20.11983
6 260.1250 25.81697 25.65382 25.66119 25.81693 25.64116 25.66089 24.04380 25.41027 24.99880 21.71813 20.11003 21.28105

我想按如下方式更改数据帧的顺序:

JulDay i46j8k1 i46j8k2 i46j8k3 i46j8k4 i47j8k1 i47j8k2 i47j8k3 i47j8k4 i48j8k1 i48j8k2 i48j8k3 i48j8k4

任何人都可以建议我怎么做?

感谢。

1 个答案:

答案 0 :(得分:5)

数据帧的顺序可以更改如下:

new<- nnn[,c("JulDay","i46j8k1","i46j8k2","i46j8k3","i46j8k4","i47j8k1","i47j8k2","i47j8k3","i47j8k4",
                   "i48j8k1","i48j8k2","i48j8k3","i48j8k4")]

感谢Arun的建议。