I have a list of substrings with the following pattern:
my.list <- list("file1\\subfile1-D.ext", "file12\\subfile9-D.ext", "file2\\subfile113-D.ext")
and so on. I'd like to extract the file numbers and the subfile-numbers into a numeric data frame containing the file/subfile numbers. So far, I've been using the following approach:
extract.file <- function(file.name){
file.name <- sub("file", "", file.name)
file.name <- sub("\\\\*subfile.*", "", file.name)
}
extract.subfile <- function(subfile.name){
subfile.name <- sub("file.*subfile", "", subfile.name)
subfile.name <- sub("-D.ext", "", subfile.name)
}
name.file <- lapply(my.list, extract.file)
name.file <- as.numeric(unlist(name.file))
name.subfile <- lapply(my.list, extract.subfile)
name.subfile <- as.numeric(unlist(name.subfile))
my.df <- data.frame(file=name.file, subfile=name.subfile)
I've also played around with first extracting the string locations with substring.location from stringr library (which yields another list with start and end values), and then looping over the two lists, but this gets too complicated again. Is there a better way to achieve the goal?