0

I want to rename my columns cause it's too long, for example:

chrX:99883666-99894988_TSPAN6_ENSG00000000003.10 to TSPAN6

chrX:99839798-99854882_TNMD_ENSG00000000005.5 to TNMD

chr20:49505584-49575092_DPM1_ENSG00000000419.8 to DPM1

How can I rename it consider the elements I want to delete differs from every columns?

zx8754
  • 52,746
  • 12
  • 114
  • 209

2 Answers2

1

Using strsplit we can try:

names(df) <- strsplit(names(df), "_")[[1]][2]

If you only want to target a certain subset of names, then simply filter names(df) using that logic.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You can do that using regex. How about extracting a word between two underscores ?

x <- c("chrX:99883666-99894988_TSPAN6_ENSG00000000003.10", 
       "chrX:99839798-99854882_TNMD_ENSG00000000005.5", 
       "chr20:49505584-49575092_DPM1_ENSG00000000419.8")

sub('.*?_(\\w+)_.*', '\\1', x)
#[1] "TSPAN6" "TNMD"   "DPM1" 

For names of the column you can use names(df) instead of x.

names(df) <- sub('.*?_(\\w+)_.*', '\\1', names(df))

and if you prefer dplyr -

library(dplyr)
df <- df %>% rename_with(~sub('.*?_(\\w+)_.*', '\\1', .))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213