ifreak
ifreak

Reputation: 1756

create a new column that arrange data frame order based on previous columns

i have a data frame like this:

  A     B      C    
1   1   1 0.4519
2 101   1 0.3819
3 201   1 0.3819
4 301   1 0.2819
5 401   1 0.9819
6 501   1 0.6819

it's larger but that's an example.

i want to create a new column called order which include a number from (1 until nrow(df)) and it increase based on the value of the C column (1 for smallest value and increase with increasing C value). and when the values in column C are equal change the ordering criteria to column A and when the values in column A are equal, change it to column B.

is this can be done in R in an easy and efficient way?

this can be done using a for loop on the data frame and make some if statement, but it will take to much time to finish. that's why i need a faster alternative if possible

thank you

Upvotes: 1

Views: 3361

Answers (1)

betabandido
betabandido

Reputation: 19704

You can use order to sort data based on several columns. According to order documentation:

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.

This would be your sample data frame:

df <- data.frame(list(A=c(1, 101, 201, 301, 401, 501),
                      B=rep(1, 6),
                      C=c(0.45, 0.38, 0.38, 0.28, 0.98, 0.68)))

And this is the code that creates a new column in the data frame based on the order you want:

df$order <- order(df$C, df$A, df$B)

Upvotes: 3

Related Questions