2015-10-30 36 views
1

列我如何重新排序我在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") 

回答

0

一种选择将是matchmonth.name与数据集的列名并用它作为索引列重新排序

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

或者,我们可以通过在原始数据集转换“月”列factor避免了这一点,并为month.name指定水平,我们做table之前。

+1

非常感谢! – Akshit