如何在数据框中重新排列列? [英] How does one reorder columns in a data frame?

查看:221
本文介绍了如何在数据框中重新排列列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改此输入(顺序为:时间,入,出,文件):

How would one change this input (with the sequence: time, in, out, files):

Time   In    Out  Files
1      2     3    4
2      3     4    5

输出(文件中的时间,时间,文件)?

To this output (with the sequence: time, out, in, files)?

Time   Out   In  Files
1      3     2    4
2      4     3    5






R数据:


Here's the dummy R data:

table <- data.frame(Time=c(1,2), In=c(2,3), Out=c(3,4), Files=c(4,5))
table
##  Time In Out Files
##1    1  2   3     4
##2    2  3   4     5


推荐答案

四列如此 df [,c(1,2,3,4)]
注意第一个逗号表示保留所有行,1,2,3,4表示列。

Your dataframe has four columns like so df[,c(1,2,3,4)]. Note the first comma means keep all the rows, and the 1,2,3,4 refers to the columns.

要更改顺序,如上面的问题做 df2 [,c(1,3,2,4)]

To change the order as in the above question do df2[,c(1,3,2,4)]

如果要输出这个文件作为一个csv,做 write.csv(df2,file =somedf.csv)

If you want to output this file as a csv, do write.csv(df2, file="somedf.csv")

这篇关于如何在数据框中重新排列列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆