如何重新排序表中的列

时间:2015-10-30 07:31:09

标签: r

如何在R中对表格的列重新排序。等级是"表格"

>dput(booking_com_graph)
 structure(c(75L, 99L, 3L, 0L, 51L, 121L, 101L, 94L, 12L, 0L, 
 46L, 170L, 92L, 143L, 2L, 0L, 43L, 213L, 90L, 80L, 32L, 26L, 
 27L, 157L, 90L, 126L, 14L, 0L, 50L, 165L), .Dim = c(6L, 5L), .Dimnames = structure(list(
    c("Brand", "Direct", "Marketing", "Referral", 
    "SEO", "Train"), c("August", "July", "June", "October", 
    "September")), .Names = c("", "")), class = "table")

1 个答案:

答案 0 :(得分:0)

一个选项是match month.name,其中包含数据集的列名,并将其用作索引以对列重新排序

df1 <- booking_com_graph[,match(month.name, colnames(booking_com_graph), nomatch=0)]

或者我们可以通过转换“月份”来避免这种情况。将原始数据集中的列添加到factor,并在执行month.name之前将级别指定为table