Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中按列对表进行排序?_R_Sorting - Fatal编程技术网

如何在R中按列对表进行排序?

如何在R中按列对表进行排序?,r,sorting,R,Sorting,我使用R包nycflights13,并以这种方式制作了一张桌子 rk <- rank(-tab.0); rk carrier.top10 <- names(tab.0)[rk<=10] flights.1 <- subset(flights, carrier %in% carrier.top10) flights.1$badflight <- as.factor(flights.1$dep_delay >= 60) levels(flights.1$badf

我使用R包nycflights13,并以这种方式制作了一张桌子

rk <- rank(-tab.0); rk 
carrier.top10 <- names(tab.0)[rk<=10]
flights.1 <- subset(flights, carrier %in% carrier.top10)
flights.1$badflight <- as.factor(flights.1$dep_delay >= 60)
levels(flights.1$badflight) <- c("good", "bad")
tab.2 <- with(flights.1, table(carrier, badflight))
tab.2m <- addmargins(tab.2)
2*prop.table(tab.2m, 1)

    badflight
carrier       good        bad        Sum
    9E  0.88567983 0.11432017 1.00000000
    AA  0.93662169 0.06337831 1.00000000
    B6  0.91406524 0.08593476 1.00000000
    DL  0.94348946 0.05651054 1.00000000
    EV  0.86400810 0.13599190 1.00000000
    MQ  0.91904781 0.08095219 1.00000000
    UA  0.93275151 0.06724849 1.00000000
    US  0.96080109 0.03919891 1.00000000
    VX  0.92886377 0.07113623 1.00000000
    WN  0.91028718 0.08971282 1.00000000
    Sum 0.91787917 0.08212083 1.00000000
现在,我想对这个文件进行降序排序。
我能做什么?

按列排序通常通过使用顺序/排序来完成:

mtcars[order(mtcars$mpg), ]
当您使用prop.table时,不会得到正常的data.frame或矩阵。您需要将其转换为一,然后使用order。试一试


Hello@hyeonny,为了获得更多帮助,请提供指定的可复制示例。
data <- 2*prop.table(tab.2m, 1)
temp <- as.data.frame.matrix(data)
temp[order(temp$good, decreasing = TRUE), ]
data <- prop.table(with(mtcars, table(cyl, am)), 1)
temp <- as.data.frame.matrix(data)
temp[order(temp$`0`, decreasing = TRUE), ]

#          0         1
#8 0.8571429 0.1428571
#6 0.5714286 0.4285714
#4 0.2727273 0.7272727