Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to separate strings to have required part in R?

I have created a list.files using the following code

#Make a list of the files
files <- list.files(path="E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB", 
                    pattern=glob2rx("*.tif$*"), full.names=TRUE)

files

 [1] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_03.APR.2022_ET.tif"
 [2] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_03.MAR.2022_ET.tif"
 [3] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_04.DEC.2021_ET.tif"
 [4] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_06.FEB.2022_ET.tif"
 [5] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_10.MAR.2022_ET.tif"
 [6] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_11.APR.2022_ET.tif"
 [7] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_13.DEC.2021_ET.tif"
 [8] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_15.FEB.2022_ET.tif"
 [9] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_18.NOV.2021_ET.tif"
[10] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_19.MAR.2022_ET.tif"
[11] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_20.DEC.2021_ET.tif"
[12] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_22.FEB.2022_ET.tif"
[13] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_26.MAR.2022_ET.tif"
[14] "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_30.JAN.2022_ET.tif"

Now I want to extract only TSEB_03.APR.2022_ET part (part 7) for all the 14 lines and make like the following

c("TSEB_03.APR.2022_ET", "TSEB_03.MAR.2022_ET", "TSEB_04.DEC.2021_ET", 
  "TSEB_06.FEB.2022_ET", "TSEB_10.MAR.2022_ET", "TSEB_11.APR.2022_ET", 
  "TSEB_13.DEC.2021_ET", "TSEB_15.FEB.2022_ET", "TSEB_18.NOV.2021_ET", 
  "TSEB_19.MAR.2022_ET", "TSEB_20.DEC.2021_ET", "TSEB_22.FEB.2022_ET", 
  "TSEB_26.MAR.2022_ET", "TSEB_30.JAN.2022_ET")

How can I do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use basename + gsub:

x <- c("E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_03.APR.2022_ET.tif",
  "E:\\ICAR PDF\\Data\\Tridip\\Desktop\\TSEB/TSEB_03.MAR.2022_ET.tif")

gsub("\\.tif$", "", basename(x))
#[1] "TSEB_03.APR.2022_ET" "TSEB_03.MAR.2022_ET"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading