order(base)
order()所属R语言包:base
Ordering Permutation
排列置换
译者:生物统计家园网 机器人LoveR
描述----------Description----------
order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort.list is the same, using only one argument.<br> See the examples for how to use these functions to sort data frames, etc.
order返回一个排列升序或降序排列,打破关系进一步论证,重新设置了它的第一个参数。 sort.list是相同的,只使用一个参数。参考,请参阅如何使用这些功能来排序数据框等例子
用法----------Usage----------
order(..., na.last = TRUE, decreasing = FALSE)
sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE,
method = c("shell", "quick", "radix"))
参数----------Arguments----------
参数:...
a sequence of numeric, complex, character or logical vectors, all of the same length, or a classed R object.
一系列数字,复杂的,性格或逻辑的向量,所有相同的长度,或归类的R对象。
参数:x
an atomic vector.
一个原子的向量。
参数:partial
vector of indices for partial sorting. (Non-NULL values are not implemented.)
向量的部分分类指数。 (非NULL值还没有实现。)
参数:decreasing
logical. Should the sort order be increasing or decreasing?
逻辑。排序顺序应该是增加或减少?
参数: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如果,他们将被删除。
参数:method
the method to be used: partial matches are allowed.
要使用的方法:允许部分匹配。
Details
详情----------Details----------
In the case of ties in the first vector, values in the second are used to break the ties. If the values are still tied, values in the later arguments are used to break the tie (see the first example). The sort used is stable (except for method = "quick"), so any unresolved ties will be left in their original ordering.
在第一个向量关系的情况下,在第二个值是用来打破的关系。如果值仍然并列,在以后的参数值是用来打破平局(见第一个例子)。使用排序是稳定的(除了method = "quick"),所以任何悬而未决的关系,将留在原来的顺序。
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.
特征向量的排序顺序将取决于中使用的语言环境的整理顺序:看到Comparison。
The default method for sort.list is a good compromise. Method "quick" is only supported for numeric x with na.last=NA, and is not stable, but will be faster for long vectors. Method "radix" is only implemented for integer x with a range of less than 100,000. For such x it is very fast (and stable), and hence is ideal for sorting factors.
sort.list的默认方法是一个很好的妥协。方法"quick"只支持数字xna.last=NA,并不稳定,但将是长期向量更快。方法"radix"只实施整数x不少于10万的范围内。对于这样的x这是非常快速和稳定,因此是理想的因素进行排序。
partial = NULL is supported for compatibility with other implementations of S, but no other values are accepted and ordering is always complete.
partial = NULL支持的其他实现兼容,但没有其他值被接受和订购始终是完整的。
For a classed R object, the sort order is taken from xtfrm: as its help page notes, this can be slow unless a suitable method has been defined or is.numeric(x) is true. For factors, this sorts on the internal codes, which is particularly appropriate for ordered factors.
一类R对象,排序顺序取自xtfrm:为帮助页的笔记,这可能是缓慢的,除非一个合适的方法已被定义或is.numeric(x)是真正的。由于这些因素,这种种内部的代码,这是特别下令因素适当。
注意----------Note----------
sort.list can get called by mistake as a method for sort with a list argument, and gives a suitable error message for list x.
sort.list可以调用错误作为sort参数列表的方法,并给出了一个合适的错误消息列表x。
参考文献----------References----------
The New S Language. Wadsworth & Brooks/Cole.
参见----------See Also----------
sort, rank, xtfrm.
sort,rank,xtfrm。
举例----------Examples----------
require(stats)
(ii <- order(x <- c(1,1,3:1,1:4,3), y <- c(9,9:1), z <-c(2,1:9)))
## 6 5 2 1 7 4 10 8 3 9[#6 5 2 1 7 4 10 8 3 9]
rbind(x,y,z)[,ii] # shows the reordering (ties via 2nd & 3rd arg)[显示重新排序(通过第二个和第三个参数的关系)]
## Suppose we wanted descending order on y.[#假设我们想在Y降序排列。]
## A simple solution for numeric 'y' is[#一个简单的解决方案是数字Y]
rbind(x,y,z)[, order(x, -y, z)]
## More generally we can make use of xtfrm[更普遍的是,我们可以使xtfrm]
cy <- as.character(y)
rbind(x,y,z)[, order(x, -xtfrm(cy), z)]
## Sorting data frames:[#排序数据框:]
dd <- transform(data.frame(x,y,z),
z = factor(z, labels=LETTERS[9:1]))
## Either as above {for factor 'z' : using internal coding}:[#无论是作为上述{}因子“Z”:使用内部编码:]
dd[ order(x, -y, z) ,]
## or along 1st column, ties along 2nd, ... *arbitrary* no.{columns}:[#或沿第一列,沿着第二的关系,... *任意* {列}。]
dd[ do.call(order, dd) ,]
set.seed(1)# reproducible example:[重复的例子:]
d4 <- data.frame(x = round( rnorm(100)), y = round(10*runif(100)),
z = round( 8*rnorm(100)), u = round(50*runif(100)))
(d4s <- d4[ do.call(order, d4) ,])
(i <- which(diff(d4s[,3]) == 0))
# in 2 places, needed 3 cols to break ties:[在2个地方,需要3 COLS打破关系:]
d4s[ rbind(i,i+1), ]
## rearrange matched vectors so that the first is in ascending order[#重新匹配的向量,使第一个是升序]
x <- c(5:1, 6:8, 12:9)
y <- (x - 5)^2
o <- order(x)
rbind(x[o], y[o])
## tests of na.last[#测试na.last]
a <- c(4, 3, 2, NA, 1)
b <- c(4, NA, 2, 7, 1)
z <- cbind(a, b)
(o <- order(a, b)); z[o, ]
(o <- order(a, b, na.last = FALSE)); z[o, ]
(o <- order(a, b, na.last = NA)); z[o, ]
## Not run: [#无法运行:]
## speed examples for long vectors:[#加快长期向量的例子:]
x <- factor(sample(letters, 1e6, replace=TRUE))
system.time(o <- sort.list(x)) ## 0.4 secs[#0.4秒]
stopifnot(!is.unsorted(x[o]))
system.time(o <- sort.list(x, method="quick", na.last=NA)) # 0.1 sec[0.1秒]
stopifnot(!is.unsorted(x[o]))
system.time(o <- sort.list(x, method="radix")) # 0.01 sec[0.01秒]
stopifnot(!is.unsorted(x[o]))
xx <- sample(1:26, 1e7, replace=TRUE)
system.time(o <- sort.list(xx, method="radix")) # 0.1 sec[0.1秒]
xx <- sample(1:100000, 1e7, replace=TRUE)
system.time(o <- sort.list(xx, method="radix")) # 0.5 sec[0.5秒]
system.time(o <- sort.list(xx, method="quick", na.last=NA)) # 1.3 sec[1.3秒]
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|