简体   繁体   English

如何根据1行中1列中的值将ID分配给多行,从而复制R中另一行中DIFFERENT列中的值?

[英]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.

相关问题 根据列值合并行,并在下一行中为不同的列值 - Merge rows based on column value and value different column in next row 对于 R 中的多列,如何按组从另一行值中减去另一行值并将值分配给同一列中的不同行 - How to subtract one row value from another and assign the value to a different row all in the same column, by group, for multiple columns in R 将不同的列值行求和到R中的新列行 - sum different column value rows into a new column row in R R-如何根据不同列中的值将特定列中的值移动到不同行 - R- How to move value in a specific column to a different row based on a value in a different column 根据 R 中的相似性将值添加到不同行的列 - Add a value to a column on a different row based on similarities in R 如何将不同行的排名值分配给新列? - How to assign different row‘s rank value to new column? 如何根据Column中的值将行合并到R中的一行 - How to merge rows to one row in R based on a value in a Column 如何使用R将包含一列相同值但其他列不同的行转换为一行? - How to convert rows that contain same value for one column but different for other columns into one single row using R? 如何获取r中不同参数的行和列值? - how to get row and column value of different parameter in r? 如何计算R中具有不同行值的列中2个单元格的总和 - how to calculate the sum of 2 cells in a column with a different row value in R
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM