简体   繁体   中英

How to assign ID to multiple rows based on a value in 1 column in 1 row duplicating a value in a DIFFERENT column in a different row in R?

When a call is placed to an emergency line, it is given a CallNo (a unique to the event); however, sometimes, multiple calls are placed and different call takers accidentally assign them different call numbers. Later, the CallNo of the other call (the DupCallNo) is appended on to EACH call.

I have two columns, CallNo and DupCallNo, plus many other variables:

CallNo  DupCallNo   Priority       Unit   
   123        255          A    Bravo12    
   255        123          A    Bravo44
   366        476          B     Xray22
   476        366          A    Xray109
   512        366          A    Xray116

How can I assign a unique ID to the first two rows and another to the second two rows?

I have found several questions and answers regarding making a unique ID based on values in the same column, but on those for two different rows with different columns. In this case, if column A in row 1 equals column B in row, how to assign rows 1 and 2 a unique ID?

Thanks so much, from an R novice.

PS Here is an example of what I would like to end up with:

CallNo  DupCallNo   Priority       Unit   UNIQUE_ID
   123        255          A    Bravo12       call1
   255        123          A    Bravo44       call1
   366        476          B     Xray22       call2
   476        366          A    Xray109       call2
   512        366          A    Xray116       call2 

How about creating a unique ID from the two columns:

library(tidyverse)

df %>% rowwise() %>%  
  mutate(Combined = paste0(min(CallNo, DupCallNo, na.rm = TRUE), max(CallNo,DupCallNo, na.rm = TRUE))) 

# A tibble: 4 x 5
# Groups:   Combined [2]
  CallNo DupCallNo Priority Unit    Combined
   <int>     <int> <fct>    <fct>   <chr>   
1    123       255 A        Bravo12 123255  
2    255       123 A        Bravo44 123255  
3    366       476 B        Xray22  366476  
4    476       366 A        Xray109 366476 

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