Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

F3-mu.rank

Ranks of Data


Description

Returns a vector of the (mid-) ranks of the input.

Usage

mu.rank(x, na.last = TRUE, na.rm=Inf)
mu.rank.nna(x)

Arguments

x

numeric vector. Missing values (NA) are allowed for mu.rank but not for mu.rank.nna

na.last

vector with one element. If TRUE, NAs are put last, if FALSE, NAs are put first, if NA, NAs are handled according to na.rm; "keep" is equivalent to NA and na.rm = F.

na.rm

logical flag, indicating if missing values (NA) should be removed (TRUE) or not (FALSE) in the output. If NA, NAs in x are not allowed and na.last is ignored. The default for na.rm is TRUE if na.last = NA and FALSE else.

Details

mu.rank is faster than rank. The treatment of missing values is controlled by both na.last and na.rm.

Value

the ranks; i.e., the i-th value is the rank of x[i]. In case of ties, average ranks is returned.

Author(s)

Knut M. Wittkowski kmw@rockefeller.edu, Tingting Song ttsong@gmail.com

See Also

Examples

a <- c(4, 2, 5, 1, 4, NA, 6)
mu.rank(a) # default: na.last=TRUE, na.rm=FALSE
# [1] 3.5 2.0 5.0 1.0 3.5 7.0 6.0
mu.rank(a,na.last=NA) # default: na.rm=TRUE
# [1] 3.5 2.0 5.0 1.0 3.5     6.0
mu.rank(a,na.last=NA,na.rm=FALSE)
#     3.5 2.0 5.0 1.0 3.5  NA 6.0

# Spearman's rank correlation between two sets of testscores 
a <- c(4, 2, 5, 1, 4, NA, 6)
b <- c(4, 2, 5, NA, 4, 5, 6)

cor(a, b, if(is.R()) "complete.obs" else "available") 
# [1] 0.8241688
cor(a, b, if(is.R()) "pairwise.complete.obs" else "omit")
# [1] 1

cor(rank(a), rank(b))
# [1] 0.1651446
cor(mu.rank(a, na.last=NA, na.rm=FALSE),
    mu.rank(b, na.last=NA, na.rm=FALSE), 
    if(is.R()) "complete.obs" else "available")
# [1] 0.8523852 
cor(mu.rank(a, na.last=NA, na.rm=FALSE),
    mu.rank(b, na.last=NA, na.rm=FALSE), 
    if(is.R()) "pairwise.complete.obs" else "omit")
# [1] 0.9953452
cor(rank(a[!is.na(a*b)]), rank(b[!is.na(a*b)]))
# [1] 1

muStat

Prentice Rank Sum Test and McNemar Test

v1.7.0
GPL (>= 2)
Authors
Knut M. Wittkowski <kmw@rockefeller.edu> and Tingting Song <ttsong@gmail.com>
Initial release
2010-09-17

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.