Home > Net >  Extract a certain part of a string separated with "/"
Extract a certain part of a string separated with "/"

Time:08-27

I have a list of file names

fastq/SRR17405580 
fastq/SRR17405578 
fastq/SRR17405579

I want to change their name to look like

SRR17405580 
SRR17405578 
SRR17405579

CodePudding user response:

You could use basename() to remove all of the path up to and including the last path separator.

x <- c("fastq/SRR17405580", "fastq/SRR17405578", "fastq/SRR17405579")

basename(x)

# [1] "SRR17405580" "SRR17405578" "SRR17405579"

You could also use regex:

sub('. /(. )', '\\1', x)

# [1] "SRR17405580" "SRR17405578" "SRR17405579"

CodePudding user response:

Another option is to use word from stringr package:

library(stringr)
word(x, 2, sep="/")

[1] "SRR17405580" "SRR17405578" "SRR17405579"
  •  Tags:  
  • r
  • Related