根据字符值创建外观的数字顺序

时间:2019-06-28 19:28:56

标签: r dplyr

我需要根据数据帧中第n次出现“ appln_id”的次数来创建外观的数字顺序。列“ numorder”是我想要得到的:

appln_id numberclass    weight        order  numorder
1   1       558       0.10000000         1         1
2   1       558       0.10000000         2         2
3   1       558       0.10000000         3         3
4   1       558       0.10000000         4         4
5   1       558       0.10000000         5         5
6   2        88       0.00435817         6         1
7   2       282       0.00435817         7         2 
8   2       282       0.00435817         8         3
9   2       282       0.00435817         9         4
10  2       282       0.00435817         10        5 

我确信dplyr可以解决问题,但是我找不到能够创建这种数字顺序的函数。

dput(mini)
    structure(list(appln_id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2), numberclass = c(558L, 
    558L, 558L, 558L, 558L, 88L, 282L, 282L, 282L, 282L), weight = c(0.1, 
    0.1, 0.1, 0.1, 0.1, 0.00435816993464052, 0.00435816993464052, 
    0.00435816993464052, 0.00435816993464052, 0.00435816993464052
    ), order = 1:10), row.names = c(NA, -10L), class = c("data.table", 
    "data.frame"))

2 个答案:

答案 0 :(得分:2)

dplyr的可能性:

df %>%
 group_by(appln_id) %>%
 mutate(numorder = row_number())

   appln_id numberclass  weight order numorder
      <dbl>       <int>   <dbl> <int>    <int>
 1        1         558 0.1         1        1
 2        1         558 0.1         2        2
 3        1         558 0.1         3        3
 4        1         558 0.1         4        4
 5        1         558 0.1         5        5
 6        2          88 0.00436     6        1
 7        2         282 0.00436     7        2
 8        2         282 0.00436     8        3
 9        2         282 0.00436     9        4
10        2         282 0.00436    10        5

或者:

df %>%
 group_by(appln_id) %>%
 mutate(numorder = 1:n())

答案 1 :(得分:1)

mini[, numorder := seq_len(.N), by = "appln_id"]
#     appln_id numberclass     weight order numorder
#  1:        1         558 0.10000000     1        1
#  2:        1         558 0.10000000     2        2
#  3:        1         558 0.10000000     3        3
#  4:        1         558 0.10000000     4        4
#  5:        1         558 0.10000000     5        5
#  6:        2          88 0.00435817     6        1
#  7:        2         282 0.00435817     7        2
#  8:        2         282 0.00435817     8        3
#  9:        2         282 0.00435817     9        4
# 10:        2         282 0.00435817    10        5