Assign unique IDs by group, according to pattern

后端 未结 2 1837
萌比男神i
萌比男神i 2021-01-21 21:48

I have a grouped dataset, but the groups don\'t have a unique identifier, like this:

direction <- c(\'N\',\'S\',\'W\',\'N\',\'N\',\'S\',\'W\',\'N\',\'S\',\'W\         


        
2条回答
  •  被撕碎了的回忆
    2021-01-21 22:29

    You can get this result with == to identify elements with "N" and cumsum to construct indices. Then pull out the values from the stored vector LETTERS as suggested in balter's answer.

    Here, cumsum sums over a logical vector, coercing it to numeric binary (1s, and 0s). It thus repeats the same value and increments any time an "N" was encountered.

     x$ID <- LETTERS[cumsum(x$direction == "N")]
    

    This returns

    x
       direction measurement ID
    1          N           4  A
    2          S           6  A
    3          W           1  A
    4          N           7  B
    5          N           2  C
    6          S           4  C
    7          W           7  C
    8          N           4  D
    9          S           1  D
    10         W           4  D
    

提交回复
热议问题