Data Manipulations

unique() returns the values of the input without any replications.

rev() reverts the order of a vector.

sort() sorts data in ascending order.

rev(sort(x)) sorts in descending order.

rank() returns the ranks of the input In case of ties, the average of the ranks is returned.

order() returns the indices of the data in ascending order The first element order(x) tells you where the lowest value in x is, the second element tells you where the second lowest value in x is, etc. sort() is equivalent to x[order(x)].

rle() computes the length and the value of runs of the same value in a vector.

For example,

> x_c(rep(1,3),seq(1,5,by=2),rev(seq(1,5,length=3)),rep(2,3))
> x
[1] 1 1 1 1 3 5 5 3 1 2 2 2

> unique(x)
[1] 1 3 5 2                     
> sort(x)
[1] 1 1 1 1 1 2 2 2 3 3 5 5
> rank(x)
[1] 3.0 3.0 3.0 3.0 9.5 11.5 11.5 9.5 3.0 7.0 7.0 7.0
> order(x)
[1] 1 2 3 4 9 10 11 12 5 8 6 7
> rle(x)
$lengths:                    
runs
[1] 4 1 2 1 1 3                
$values:                    
[1] 1 3 5 3 1 2                
We have 4 one's, 1 three, 2 fives, ...

These commands can be applied to matrices:

> a1_c(130,26,140,110,24,155,118,25,142,112,25,175,128,26,170)
> measu_matrix(a1,ncol=3,byrow=T)
> dimnames(measu)_list(c(NULL),   c("Weight","Waist","Heights"))
> measu
     Weight Waist Heights
[1,]    130    26     140
[2,]    110    24     155
[3,]    118    25     142
[4,]    112    25     175
[5,]    128    26     170 
>  i_order(measu[,1])
> i
[1] 2 4 3 5 1 
> measu[i,]
 Weight Waist Heights
    110    24     155
    112    25     175
    118    25     142
    128    26     170
    130    26     140 
We get the people ordered by weight.


Comments to: Miguel A. Arcones

Go to main homepage: