0

I have a list of file names enter image description here and want to string extract just the part of the name before the _ enter image description here

I tried using the following but was unsuccessful.

condition <- strsplit(count_files, "_*")

also tried

condition <- strsplit(count_files, "_*.[c,t]sv")

Any suggestions?

1 Answers1

3

Just use trimws from base R

trimws(count_files, whitespace = "_.*")
[1] "Fibroblast" "Fibroblast"

The output from strsplit is a list, it may need to be unlisted. Also, when we use _* the regex mentioned is zero or more _. Instead, it should be _.* i.e. _ followed by zero or more other characters (.*)

unlist(strsplit(count_files, "_.*"))

data

count_files <- c("Fibroblast_1.csv", "Fibroblast_2.csv")
akrun
  • 874,273
  • 37
  • 540
  • 662