R 将数据集中的列从指定行中的最大值排序到最小值

R 将数据集中的列从指定行中的最大值排序到最小值,r,data-manipulation,R,Data Manipulation,假设我有一个数据集: library(data.table) dat1<-data.table(id=c(111,111,111,222,222,222), year=c(1995,1996,1997,1995,1996,1997), value=c(4,5,1,2,6,7)) 库(data.table) dat1您可以执行setcolorder setcolorder(dat1, names(dat1)[order(dat1[1])]) dat1 value id year

假设我有一个数据集:

library(data.table)

dat1<-data.table(id=c(111,111,111,222,222,222), year=c(1995,1996,1997,1995,1996,1997), value=c(4,5,1,2,6,7))
库(data.table)

dat1您可以执行
setcolorder

setcolorder(dat1, names(dat1)[order(dat1[1])])
dat1
   value  id year
1:     4 111 1995
2:     5 111 1996
3:     1 111 1997
4:     2 222 1995
5:     6 222 1996
6:     7 222 1997
方法2

dat1 = dat1[,names(dat1)[order(dat1[1,])],with=F]
   value  id year
1:     4 111 1995
2:     5 111 1996
3:     1 111 1997
4:     2 222 1995
5:     6 222 1996
6:     7 222 1997
罗纳克沙推荐

dat1[, order(dat1[1, ]), with = FALSE]

您可以执行
setcolorder

setcolorder(dat1, names(dat1)[order(dat1[1])])
dat1
   value  id year
1:     4 111 1995
2:     5 111 1996
3:     1 111 1997
4:     2 222 1995
5:     6 222 1996
6:     7 222 1997
方法2

dat1 = dat1[,names(dat1)[order(dat1[1,])],with=F]
   value  id year
1:     4 111 1995
2:     5 111 1996
3:     1 111 1997
4:     2 222 1995
5:     6 222 1996
6:     7 222 1997
罗纳克沙推荐

dat1[, order(dat1[1, ]), with = FALSE]

我知道示例在data.table中,但您知道如何使用“order”命令在base R中执行此操作吗?@RonakShah谢谢:-)您介意我更新它吗?我知道示例在data.table中,但您知道如何使用“order”命令在base R中执行此操作吗?@RonakShah谢谢:-)您介意我更新它吗?