使用 R [重复] 更改数据框中列的顺序

Posted

技术标签:

【中文标题】使用 R [重复] 更改数据框中列的顺序【英文标题】:Change the order of the columns in a dataframe using R [duplicate] 【发布时间】:2013-04-09 11:45:33 【问题描述】:

我有一个数据如下:

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

我想改变dataframe的顺序如下:

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

谁能建议我该怎么做?

谢谢。

【问题讨论】:

只需:nnn[, c("col1", "col2")] 其中c(.) 是您显示的所需列顺序。 感谢阿伦。它解决了这个问题。你能在答案上写同样的话,以便我可以接受。 jdbaba,你可以自己写答案并标记为已回答,我完全没问题。 【参考方案1】:

dataframe的顺序可以改变如下:

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

感谢 Arun 的建议。

【讨论】:

以上是关于使用 R [重复] 更改数据框中列的顺序的主要内容,如果未能解决你的问题,请参考以下文章

报告pyspark数据框中列的前3个最大值[重复]

更改数据框中列的元素并合并列

我可以更改 Spark 数据框中列的可空性吗?

给定列名,数据框中列的平均值

如何从图形的整数更改 r 中列的名称

如何使用Scala计算Spark中数据框中列的开始索引和结束索引之间的行的平均值?