[英]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); 当呼叫被拨到紧急线路时,会被赋予CallNo(事件唯一); 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.
以后,另一个呼叫(DupCallNo)的CallNo被附加到EACH呼叫上。
I have two columns, CallNo and DupCallNo, plus many other variables: 我有两列,CallNo和DupCallNo,以及许多其他变量:
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? 如何为前两行分配一个唯一的ID,为后两行分配另一个?
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. 我发现了一些有关基于同一列中的值但基于具有不同列的两个不同行的值来创建唯一ID的问题和答案。 In this case, if column A in row 1 equals column B in row, how to assign rows 1 and 2 a unique ID?
在这种情况下,如果第1行中的A列等于第B行中的B列,那么如何为第1行和第2行分配唯一的ID?
Thanks so much, from an R novice. 非常感谢R新手。
PS Here is an example of what I would like to end up with: PS这是我最后想得到的一个例子:
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: 如何从两列创建唯一的ID:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.