Deepesh
Deepesh

Reputation: 840

Extracting a numeric value from multiple filename in R

I'm trying to extract a numeric value from multiple filenames, for instance I have filenames such as abc_2.csv; pow_4.csv; foo_5.csv...so on, I 'm trying to extract only the last numeric value from file name. I have tried extracting for one file at a time but want to do it as altogether,this is what I have tried for a

single file

>nop <- basename("D:/files/abc_2.csv")
>nop <- as.numeric(gsub("\\D+", "", nop))
>nop
  2

for multiple files

setwd("D:/files")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)

Thanks in advance...

Upvotes: 0

Views: 972

Answers (3)

akrun
akrun

Reputation: 887098

We can use regmatches/regexpr from base R

regmatches(t, regexpr( "\\d+", t))
#[1] "2" "4" "5"

If it is the last number to extract

sub(".*(\\d+)\\D+$", "\\1", t)

or

sapply(regmatches(t, gregexpr( "\\d+", t)), tail, 1)

data

t <- c("abc_2.csv","pow_4.csv","foo_5.csv")

Upvotes: 1

joel.wilson
joel.wilson

Reputation: 8413

just extending your solution :

setwd("D:/location")
temp = list.files(pattern=".*_\\d+.csv") # this will ensure only the selective files(with the specified pattern) are chosen, and not all the files in directory
unlist(lapply(temp, function(x) gsub( "(.*_|\\.csv)", "", x)))
#[1] "2" "4" "5"

Upvotes: 1

TurtleIzzy
TurtleIzzy

Reputation: 1047

You need stri_extract_last(...) from library stringi.

library('stringi')
t = c("abc_2.csv","pow_4.csv","foo_5.csv")

stri_extract_last(t, regex = "(\\d+)")

Upvotes: 2

Related Questions