基于作为输入传递的多列的订单 [英] Order based on multiple columns passed in as an input

查看:32
本文介绍了基于作为输入传递的多列的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,该函数按给定的data.frame(我将其称为dataSet)按任意数量的列进行排序,其名称也传递到该函数中(在我将引用的向量中)以作为orderList).我知道要按单个传入的字符串进行排序,就可以使用

I would like to write a function that sorts a given data.frame (which I'll refer to as dataSet) by any number of its columns, whose names are also passed into the function (in a vector which I will refer to as orderList). I know that to order by a single passed in string you can just use

sortDataset <- function(dataSet, sortCol) {
return(dataSet[order(dataSet[[sortCol]]),])
}

,并且您可以使用

sortDataset <- function(dataSet, sortCol1, sortCol2) {
return(dataSet[order(dataSet[[sortCol1]], dataSet[[sortCol2]]),])
}

有很多我想要的sortCol#输入.但是,我希望能够传递任意数量的字符串列表.我尝试了以下方法:

with however many sortCol# inputs as I would want. I would, however, like to be able to pass in a list of any number of strings. I tried the following:

dataSet[order(dataSet[[orderList]]),]
dataSet[order(dataSet$orderList),]
dataSet[order(dataSet[,orderList])]

并遇到前两个问题,因为它们不是获取多列的有效方法(尽管我仍然尝试过:):),而在第三个问题中,订单似乎不接受返回的矩阵通过dataSet [,orderList]作为参数.

and encountered issues that with the first 2, since they're just not a valid way to get multiple columns (I still tried, though ): ) and that in the third, order doesn't seem to accept the matrix returned by dataSet[,orderList] as a parameter.

我想要一个函数,如下所示:

I would like a function as follows:

sortDataset <- function(dataSet, sortCols)

其中sortCols的第一个元素是优先级最高的列,然后第二列是第一个决胜局,第三列是第二个决胜局,依此类推,该函数返回适当排序的dataSet.如果我可以在可选输入中指定每条是否应该升序,那也很好,因此第一列可以升序排序,第二列可以降序排序,等等.

where the first element of sortCols is the column which takes highest priority, then the second column is the first tiebreaker, the third column is the second tiebreaker, etc. and the function returns dataSet sorted appropriately. It would also be nice if I could specify whether each should be ascending in an optional input, so the first column could be sorting ascending, the second sorted descending, etc.

到目前为止,我真正想到的唯一方法是假设每个列表仅包含数字值,然后将各种排序列乘以10 ^ n,以便将所有列合并为一个保持优先级,然后按该列排序.不过,我觉得应该有更好的方法来执行此操作,因为这似乎是一个非常基本的功能.

So far, the only method I can really think of is to assume each list only contains numeric values, and then do some multiplying of the various sorting columns by 10^n so that all the columns can be consolidated into one column that maintains the priorities, and then sort by that column. I feel like there should be a better way to do this, though, since this seems like a pretty basic function.

推荐答案

使用 do.call :

data[do.call("order", data[sortCols]), ]

其中 data 是数据框,而 sortCols 是列名的字符向量.

where data is a data frame and sortCols is a character vector of column names.

还可以查看doBy软件包中的 orderBy .

Also have a look at orderBy in the doBy package.

这篇关于基于作为输入传递的多列的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆