简体   繁体   中英

Arrange data frame with multiple columns + rank column

I have sorted a data frame with arrange function (dplyr)

数据框

How can I add a column that shows the rank of the data frame? ( without jumping any number)

for example

row 2 and 3 are exactly same so both have rank 2

row 4 and row 5 are same in column A and B but are different in column C so

row 4: rank 3
row 5: rank 4

You can paste all the values rowwise and use match to create rank column. If your dataframe is called df try:

vec <- do.call(paste, c(df, sep = '-'))
df$rank <- match(vec, unique(vec))
df

#  X1 X2 X3 X4 rank
#1  1  2  3  4    1
#2  4  5  6  7    2
#3  1  2  3  4    1

data

df <- structure(list(X1 = c(1L, 4L, 1L), X2 = c(2L, 5L, 2L), X3 = c(3L, 
6L, 3L), X4 = c(4L, 7L, 4L)), class = "data.frame", row.names = c(NA, -3L))

We can use cur_group_id from dplyr

library(dplyr)
df %>% 
    group_by(across(everything())) %>% 
    mutate(rank = cur_group_id()) %>% 
    ungroup
# A tibble: 3 x 5
#     X1    X2    X3    X4  rank
#  <int> <int> <int> <int> <int>
#1     1     2     3     4     1
#2     4     5     6     7     2
#3     1     2     3     4     1

data

df <- structure(list(X1 = c(1L, 4L, 1L), X2 = c(2L, 5L, 2L), X3 = c(3L, 
6L, 3L), X4 = c(4L, 7L, 4L)), class = "data.frame", row.names = c(NA, -3L))

I guess you mean to rank the dataframe as a whole by apply ing some summary measures to the columns such as sum . That measure can be rank ed:

set.seed(123)
df <- data.frame(
  c1 = runif(10),
  c2 = sample(100:1000, 10),
  c3 = rnorm(10),
  c4 = sample(1:10, 10)
)

df$rank <- rank(-apply(df, 1, sum))

Result:

df
          c1  c2         c3 c4 rank
1  0.2875775 962  1.2240818  2    1
2  0.7883051 508  0.3598138  4    6
3  0.4089769 709  0.4007715  9    4
4  0.8830174 614  0.1106827  3    5
5  0.9404673 192 -0.5558411  1    9
6  0.0455565 906  1.7869131  6    3
7  0.5281055 320  0.4978505  5    8
8  0.8924190 137 -1.9666172 10   10
9  0.5514350 392  0.7013559  8    7
10 0.4566147 951 -0.4727914  7    2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM