Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reordering a paired variable

Tags:

sorting

r

matrix

I was wondering about the following thing:

I have a 16x2 matrix with in the first column numerical values and in the second column also numerical values but actually they're position numbers so they need to be treated as a factor.

I want to order the values from the first column from low to high but I need the numbers of the second column to stay with their original partner value from the first column.

So let's say you've got:

4 1
6 2 
2 3

And now I want to sort the first column from low to high.

Then I want to get

2 3
4 1
6 2

Does anybody know how I can do this?

R doesn't seem to provide a variable type for paired data...

like image 740
Bart Avatar asked Jun 24 '26 09:06

Bart


1 Answers

You can do:

dat[order(dat[, 1]), ]
like image 81
flodel Avatar answered Jun 26 '26 23:06

flodel