Rename header in R with extracted information from the header

57 Views Asked by At

How can I rename the header in R, based on information from the current header?

My df look likes:

header: (index; d__Bacteria.p__Actinobacteriota.c__Actinobacteria ; d__Bacteria.p__Bacteroidota.c__Bacteroidia )

Row 1: (BF13A; 0; 14572)

Row 2: (BF13B; 0; 24215)

etc

I want to rename the columns with taxonomy information (d__Bacteria.) to only have the information after c__

header: (index; Actinobacteria ; Bacteroidia )

Row 1: (BF13A; 0; 14572)

Row 2: (BF13B; 0; 24215)

ect.

Btw I have more columns than two with taxonomic information, so the solution such work on bigger df as well.

1

There are 1 best solutions below

0
On

You can rename the columns using tidyverse. Something like below,

library(tidyverse)
df %>% 
  rename(
    Actinobacteria = d__Bacteria.p__Actinobacteriota.c__Actinobacteria,
    Bacteroidia = d__Bacteria.p__Bacteroidota.c__Bacteroidia
    )

This can also be done using base functions

names(df)[names(df) == "d__Bacteria.p__Actinobacteriota.c__Actinobacteria"] <- "Actinobacteria"
names(df)[names(df) == "d__Bacteria.p__Bacteroidota.c__Bacteroidia"] <- "Bacteroidia"

Instead of setting the new column names, you can modify the old names like this

new_df <- df  %>% 
  setNames(substring(names(.),regexpr(".c_", names(.)) + 4)) 
> colnames(new_df)
[1] "Actinobacteria" "Bacteroidia"