I see, then take a look at the following and check if that solves your challenge:
# Load libraries ----------------------------------------------------------
library("tidyverse")
# Define example data -----------------------------------------------------
res <- tribble(
~ensembl_gene_id, ~baseMean, ~log2FoldChange, ~lfcSE, ~stat, ~pvalue, ~padj,
"ENSG00000223972", 0.592269, 2.4573801, 6.196180, 0.39659597, 0.691665426, NA,
"ENSG00000227232", 419.788972, 0.3387703, 0.382205, 0.88635750, 0.375424916, 0.7252578,
"ENSG00000238009", 0.550554, -0.0363070, 5.256634, -0.00690689, 0.994489145, NA,
"ENSG00000237683", 17.226259, 3.8523382, 1.141383, 3.37514947, 0.000737756, 0.0565244,
"ENSG00000268903", 1.548329, 2.0099032, 2.157024, 0.93179442, 0.351442781, NA,
"ENSG00000239906", 0.475776, -0.0362637, 6.765833, -0.00535983, 0.995723495, NA
)
G_list <- tribble(
~ensembl_gene_id, ~hgnc_symbol,
"ENSG00000223972", "SCYL3",
"ENSG00000227232", NA,
"ENSG00000238009", "FGR",
"ENSG00000237683", "CFH",
"ENSG00000268903", NA,
"ENSG00000239906", "NIPAL3"
)
# Wrangle data ------------------------------------------------------------
# Tibble solution
res_joined <- res %>%
full_join(G_list,
by = "ensembl_gene_id") %>%
mutate(gene_id = case_when(is.na(hgnc_symbol) ~ ensembl_gene_id,
!is.na(hgnc_symbol) ~ hgnc_symbol))
# Data frame solution
res_joined_as_df <- res_joined %>%
select(-ensembl_gene_id, - hgnc_symbol) %>%
column_to_rownames("gene_id")
Hope it helps 