基于R中最后三个字符的合并数据

时间:2017-09-18 17:54:13

标签: r merge

我想合并Airport_Code.1& Airport_Code.2由最后三个   Airport_Code.2的字母。如果来自Airport_Code.1的最后3个字母   匹配Airport_Code.2打印Airport_Code.1

        Airport_Code.1       Airport_Code.2      
    1       AJFK                 BWI              
    2       ABWI                 JSJ              
    3       BJSJ                 JFk              
    4       ....                 ...             
    5       ....                 ...             

2 个答案:

答案 0 :(得分:1)

一些数据:

a = sample(LETTERS, 20)
b = sample(LETTERS, 20)
c = sample(LETTERS, 20)
d = sample(LETTERS, 20)

Airport_Code.2 = paste0(a,b,c)
Airport_Code.1 = paste0(d,a,b,c)

df = data.frame(Airport_Code.1, Airport_Code.2)

df$Airport_Code.1 = as.character(df$Airport_Code.1)
df$Airport_Code.2 = as.character(df$Airport_Code.2)

使用nchar找出Airport_Code.1中的字母数,并使用substring隔离最后3个字母,如果匹配则使用ifelse返回Airport_Code.1如果没有。

n_air = nchar(df$Airport_Code.1)
Result_Airport = ifelse(substring(Airport_Code.1,  n_air-2, n_air) ==  Airport_Code.2, Airport_Code.1, NA)

答案 1 :(得分:0)

purrr::map解决方案

df$Result <- map_chr(1:nrow(df), ~ifelse(grepl(df[.x,2], df[.x,1]), as.character(df[.x,1]), NA))

输出

  Airport_Code.1 Airport_Code.2 Result_Airport Result
1           AJFK            JFK           AJFK   AJFK
2           ABWI            BWI           ABWI   ABWI
3           BJSJ            JSJ           BJSJ   BJSJ

您的数据

df <- read.table(text="Airport_Code.1       Airport_Code.2     Result_Airport 
   AJFK                 JFK              AJFK
  ABWI                 BWI              ABWI
  BJSJ                 JSJ              BJSJ", header=TRUE)