0
votes

I have 2 datasets (lists/columns of gene names) such as:

df1

Gene_id
SUMO2
CDC37
COPB2
BECN1
CAPNS1

and

df2

Gene_id
SUMO2
BECN1
CAPNS1

I want to make a new dataset that has 2 columns with the gene names matched up. 1 with all of df1 genes and the 2nd with all of df2 genes matched up in column 1. And NA's where column 2 does not have a match which would look like below. Preferably with dplyr in R or Python. Thanks

Gene_id Gene_id
SUMO2   SUMO2
CDC37   NA
COPB2   NA
BECN1   BECN1
CAPNS1  CAPNS1
2

2 Answers

1
votes

We can use %in%

i1 <- df1$Gene_id %in% df2$Gene_id
df1$newGene_id[i1] <- df1$Gene_id[i1]
1
votes
    df1 %>%
      mutate(Gene_id_2 = ifelse(Gene_id %in% df2$Gene_id, as.character(Gene_id), NA)) #as.character in case you deal with factors

# Gene_id Gene_id_2
# 1   SUMO2     SUMO2
# 2   CDC37      <NA>
# 3   COPB2      <NA>
# 4   BECN1     BECN1
# 5  CAPNS1    CAPNS1