如何对数据框中的列进行重新排序? [英] How does one reorder columns in a data frame?

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

问题描述

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

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

到这个输出(顺序为:time、out、in、files)?

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天全站免登陆