Altering rownames in tibble

Viewed 24

I have 2 different tibbles, and have to find out how many of the rows from the first tibble is also present in the second tibble. Both tibbles have a first column named GeneID, but the problem is, that in one tibble the genes are names as 1, 2, 3, 4 ect, and in the sencond tibble they are named Gene1, Gene2, Gene3, Gene4... Are there anyway to either add 'Gene' before the number in the first tibble or remove 'Gene' in the second?

2 Answers

It is always good to include a sample of your data so that the responders can answer correctly. For example, if the field ordering are identical between the 2 datasets, e.g. df1 and df2, you can make the names the same by a simple:

names(df1) <- names(df2)

Is this what you'd like to do?

library(tidyverse)

df1 <- tribble(
  ~gene,
  1,
  2,
  5,
  6
)

df2 <- tribble(
  ~gene,
  "Gene1",
  "Gene2",
  "Gene3",
  "Gene4",
  "Gene5"
)

# df1 rows also in df2
df1 |> 
  mutate(gene = str_c("Gene", gene)) |> 
  inner_join(df2, by = "gene")
#> # A tibble: 3 × 1
#>   gene 
#>   <chr>
#> 1 Gene1
#> 2 Gene2
#> 3 Gene5

Created on 2022-06-16 by the reprex package (v2.0.1)

Related