Reordering and sorting elements of a vector¶
set.seed(231)
x <- rnorm(n=10)
x
[1] -0.53310192 -2.31166378 -0.95419786 0.26251575 -0.47335746 0.21739728
[7] 0.06292205 -0.87782986 0.56368979 -0.03432728
x.srt <- sort(x)
x.srt
[1] -2.31166378 -0.95419786 -0.87782986 -0.53310192 -0.47335746 -0.03432728
[7] 0.06292205 0.21739728 0.26251575 0.56368979
sort(x,decreasing=TRUE)
[1] 0.56368979 0.26251575 0.21739728 0.06292205 -0.03432728 -0.47335746
[7] -0.53310192 -0.87782986 -0.95419786 -2.31166378
stex <- c("1","11","A","a","Ab","AB","ab","aB","B","b","bb")
sort(stex)
[1] "1" "11" "a" "A" "ab" "aB" "Ab" "AB" "b" "B" "bb"
set.seed(2134)
x <- rnorm(6)
x
[1] 0.6549052 -0.2099869 -0.6148580 -0.2740271 -0.7234317 1.4371483
y <- rnorm(6)
y
[1] -0.09385485 -0.05070594 0.77188553 0.36295090 1.12152639 0.72011916
ii <- order(x)
x.ordered <- x[ii]
y.ordered <- y[ii]
x.ordered
[1] -0.7234317 -0.6148580 -0.2740271 -0.2099869 0.6549052 1.4371483
y.ordered
[1] 1.12152639 0.77188553 0.36295090 -0.05070594 -0.09385485 0.72011916
jj <- order(ii)
all(x.ordered[jj] == x)
[1] TRUE
all(y.ordered[jj] == y)
[1] TRUE
- R file: reordering-and-sorting.R
- Rmarkdown file: reordering-and-sorting.Rmd
- Jupyter notebook file: reordering-and-sorting.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):