找回密码
 注册
查看: 12850|回复: 0

R语言:sort()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 22:07:01 | 显示全部楼层 |阅读模式
sort(base)
sort()所属R语言包:base

                                        Sorting or Ordering Vectors
                                         排序或排列向量

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Sort (or order) a vector or factor (partially) into ascending or descending order.  For ordering along more than one variable, e.g., for sorting data frames, see order.
排序(或命令)一个向量或因素(部分),升序或降序排列。对于沿多个变量,例如排序,排序的数据框,看到order。


用法----------Usage----------


sort(x, decreasing = FALSE, ...)

## Default S3 method:[默认方法]
sort(x, decreasing = FALSE, na.last = NA, ...)

sort.int(x, partial = NULL, na.last = NA, decreasing = FALSE,
         method = c("shell", "quick"), index.return = FALSE)



参数----------Arguments----------

参数:x
for sort an R object with a class or a numeric, complex, character or logical vector.  For sort.int, a numeric, complex, character or logical vector, or a factor.
sortR对象的一类或一个数字,复杂的,性格或逻辑向量。 sort.int,一个数字,复杂的,字符或逻辑向量,或一个因素。


参数:decreasing
logical.  Should the sort be increasing or decreasing? Not available for partial sorting.
逻辑。排序应该是增加或减少?不提供部分排序。


参数:...
arguments to be passed to or from methods or (for the default methods and objects without a class) to sort.int.
参数被传递或从方法(默认的方法,并没有一个类的对象)sort.int。


参数:na.last
for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed.
控制NA的待遇。 TRUE如果,在数据缺失值放在最后,如果FALSE,他们把第一; NA如果,他们将被删除。


参数:partial
NULL or an integer vector of indices for partial sorting.
NULL或部分分类指数的整数向量。


参数:method
character string specifying the algorithm used.  Not available for partial sorting.
用字符串指定的算法。不提供部分排序。


参数:index.return
logical indicating if the ordering index vector should be returned as well; this is only available for a few cases, the default na.last = NA and full sorting of non-factors.
逻辑表明,如果订货指数矢量应返回,这是只适用于少数情况下,默认的na.last = NA和非因素完全排序。


Details

详情----------Details----------

sort is a generic function for which methods can be written, and sort.int is the internal method which is compatible with S if only the first three arguments are used.
sort是哪些方法可以写成一个通用的功能,和sort.int是内部这是与S兼容的,如果只有前三个参数的方法。

The default sort method makes use of order for classed objects, which in turn makes use of the generic function xtfrm (and can be slow unless a xtfrm method has been defined unless is.numeric(x) is true).
默认sort方法使用order归类对象,这使得使用的通用功能反过来xtfrm(xtfrm方法已被定义,除非可以缓慢除非is.numeric(x)是真的)。

If partial is not NULL, it is taken to contain indices of elements of the result which are to be placed in their correct positions in the sorted array by partial sorting.  For each of the result values in a specified position, any values smaller than that one are guaranteed to have a smaller index in the sorted array and any values which are greater are guaranteed to have a bigger index in the sorted array.  (This is included for efficiency, and many of the options are not available for partial sorting.  It is only substantially more efficient if partial has a handful of elements, and a full sort is done (a quick sort if possible) if there are more than 10.)  Names are discarded for partial sorting.
partial如果非NULL,它被包含元素的结果是被放置在正确的位置在排序后的数组部分排序指标。对于每个结果值在指定的位置,任何值小于,一个是保证有一个较小的索引和排序后的数组中任何值,这是更大的保证有一个更大的指数在排序后的数组。 (这包括提高效率,许多选项可用于部分排序。它不仅大大更有效,如果是partial有少数元素,做一个完整的排序(快速排序,如果可能的话)如果有超过10个。)的名称被丢弃部分排序。

Complex values are sorted first by the real part, then the imaginary part.
复杂的值进行排序,第一个真正的一部分,则虚部。

The sort order for character vectors will depend on the collating sequence of the locale in use: see Comparison. The sort order for factors is the order of their levels (which is particularly appropriate for ordered factors).
特征向量的排序顺序将取决于中使用的语言环境的整理顺序:看到Comparison。为因素的排序顺序是为了他们的水平(下令因素尤其是适当的)。

Method "shell" uses Shellsort (an O(n^{4/3}) variant from Sedgewick (1996)).  If x has names a stable sort is used, so ties are not reordered.  (This only matters if names are present.)
方法"shell"使用希尔(O(n^{4/3})从Sedgewick变种(1996))。如果x的名称,一个稳定的排序,所以关系不重新排序。 (这仅事项,如果名称存在。)

Method "quick" uses Singleton's Quicksort implementation and is only available when x is numeric (double or integer) and partial is NULL.  (For other types of x Shellsort is used, silently.)  It is normally somewhat faster than Shellsort (perhaps twice as fast on vectors of length a million) but has poor performance in the rare worst case.  (Peto's modification using a pseudo-random midpoint is used to make the worst case rarer.)  This is not a stable sort, and ties may be reordered.
方法"quick"使用Singleton的快速排序实现,并且是唯一可用的时候x是数字(双或整数)partial是NULL。 (对于其他类型的x希尔被使用,悄无声息。),它通常是比希尔更快一些(也许快两倍,向量的长度为一百万),但在罕见的最坏的情况下表现不佳。 (皮托使用伪随机中点的修改是用来做最坏的情况罕见。)这是一个稳定的排序,可能会重新排序和领带。


值----------Value----------

For sort, the result depends on the S3 method which is dispatched.  If x does not have a class the rest of this section applies.  For classed objects which do not have a specific method the default method will be used and is equivalent to x[order(x,     ...)]: this depends on the class having a suitable method for [ (and also that order will work, which is not the case for a class based on a list).
,sort结果取决于S3的方法是派出。如果x不会有一个类,适用本节的其余部分。对于类对象不具有一个特定的方法,将使用默认的方法和相当于x[order(x,     ...)]:这取决于类有一个合适的方法[(order会的工作,这是不是基于列表类的情况下)。

For sort.int the value is the sorted vector unless index.return is true, when the result is a list with components named x and ix containing the sorted numbers and the ordering index vector.  In the latter case, if method ==     "quick" ties may be reversed in the ordering, unlike sort.list, as quicksort is not stable.  NB: the index vector refers to element numbers after removal of NAs.
sort.int值排序向量,除非index.return是真实的,结果是一个名为x和的组件列表ix包含排序的号码和订货指数矢量。在后一种情况下,如果method ==     "quick"可逆转的顺序关系,不像sort.list,快速排序是不是稳定的。注意:索引向量去除NA的后指元素编号。

All attributes are removed from the return value (see Becker et al, 1988, p.146) except names, which are sorted.  (If partial is specified even the names are removed.)   Note that this means that the returned value has no class, except for factors and ordered factors (which are treated specially and whose result is transformed back to the original class).
返回值(见Becker等人,1988年,第146页)除了名称,排序的所有属性将被删除。 (如果partial指定连名字都被删除。)注意,这意味着,返回值没有类的因素除外,并下令因素(特殊处理,其结果转换回原班) 。


参考文献----------References----------

The New S Language. Wadsworth & Brooks/Cole.
A new upper bound for Shell sort. J. Algorithms 7, 159–173.
minimal storage: Algorithm 347. Communications of the ACM 12, 185–187.

参见----------See Also----------

"Comparison" for how character strings are collated.
如何整理字符串比较。

order for sorting on or reordering multiple variables.
order排序或重新排列多个变量。

is.unsorted. rank.
is.unsorted。 rank。


举例----------Examples----------


require(stats)

x <- swiss$Education[1:25]
x; sort(x); sort(x, partial = c(10, 15))
median.default # shows you another example for 'partial'[显示你为部分的另一个例子]

## illustrate 'stable' sorting (of ties):[#说明“稳定”排序(关系):]
sort(c(10:3,2:12), method = "sh", index.return=TRUE) # is stable[是稳定的]
## $x : 2  3  3  4  4  5  5  6  6  7  7  8  8  9  9 10 10 11 12[#$ X:2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 12]
## $ix: 9  8 10  7 11  6 12  5 13  4 14  3 15  2 16  1 17 18 19[#$ IX:9 8 10 7 11 6 12 5 13 4 14 3 15 2 16 1 17 18 19]
sort(c(10:3,2:12), method = "qu", index.return=TRUE) # is not[是不是]
## $x : 2  3  3  4  4  5  5  6  6  7  7  8  8  9  9 10 10 11 12[#$ X:2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 12]
## $ix: 9 10  8  7 11  6 12  5 13  4 14  3 15 16  2 17  1 18 19[#$ IX:9 10 8 7 11 6 12 5 13 4 14 3 15 16 2 17 1 18 19]
##        ^^^^^[#^ ^ ^ ^ ^]

x <- c(1:3, 3:5, 10)
is.unsorted(x)                # FALSE: is sorted[是FALSE:排序]
is.unsorted(x, strictly=TRUE) # TRUE : is not (and cannot be)[真:是不是(不能)]
                              # sorted strictly[排序严格]
## Not run: [#无法运行:]
## Small speed comparison simulation:[#小速度比较模拟:]
N <- 2000
Sim <- 20
rep &lt;- 1000 # &lt;&lt; adjust to your CPU[<<调整到你的CPU]
c1 <- c2 <- numeric(Sim)
for(is in 1:Sim){
  x <- rnorm(N)
  c1[is] <- system.time(for(i in 1:rep) sort(x, method = "shell"))[1]
  c2[is] <- system.time(for(i in 1:rep) sort(x, method = "quick"))[1]
  stopifnot(sort(x, method = "s") == sort(x, method = "q"))
}
rbind(ShellSort = c1, QuickSort = c2)
cat("Speedup factor of quick sort():\n")
summary({qq <- c1 / c2; qq[is.finite(qq)]})

## A larger test[#一个更大的考验]
x <- rnorm(1e7)
system.time(x1 <- sort(x, method = "shell"))
system.time(x2 <- sort(x, method = "quick"))
stopifnot(identical(x1, x2))


转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 12:09 , Processed in 0.019861 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表