1

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

4

1 回答 1

3

您可以使用order基于多个列对数据进行排序。根据order文档:

在第一个向量中的平局的情况下,第二个向量中的值用于打破平局。如果这些值仍然绑定,则使用后面的参数中的值来打破绑定。

这将是您的示例数据框:

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)))

这是根据您想要的顺序在数据框中创建新列的代码:

df$order <- order(df$C, df$A, df$B)
于 2012-05-26T11:56:56.560 回答