Hi,
Welcome to the RStudio community!
Here is a way of doing this in base R
#Get the filenames
fileNames = list.files(path = ".", pattern = ".fcsv")
#Dummy filenames (replace by above)
fileNames = c("125975_Face_1.fcsv", "125975_Face_2.fcsv", "126284_Face_1.fcsv")
#Split the filenames by _ or .
fileNames = strsplit(fileNames, '_|\\.')
fileNames
#> [[1]]
#> [1] "125975" "Face" "1" "fcsv"
#>
#> [[2]]
#> [1] "125975" "Face" "2" "fcsv"
#>
#> [[3]]
#> [1] "126284" "Face" "1" "fcsv"
#Get the numeric values
fileNames = data.frame(
num1 = as.integer(sapply(fileNames, "[[", 1)),
num2 = as.integer(sapply(fileNames, "[[", 3))
)
fileNames
#> num1 num2
#> 1 125975 1
#> 2 125975 2
#> 3 126284 1
Created on 2022-05-26 by the reprex package (v2.0.1)
The sapply(fileNames, "[[", 1)
function is a way of getting the n-th element (here 1st) out of a nested list.
I have also added as.integer()
to convert the results to integers. If you like to keep it as character, remove that.
If you don't mind using the Tidyverse, they have some very neat functions to do this quicker using a little bit of RegEx
library(tidyverse)
fileNames = c("125975_Face_1.fcsv", "125975_Face_2.fcsv", "126284_Face_1.fcsv")
#Create a data frame with file names
fileNames = data.frame(file = fileNames)
#Use the extraxt function from the tidyr package to split into new columns
fileNames = fileNames %>%
extract(file, into = c("num1", "num2"), regex = "^(\\d+)_Face_(\\d+)", convert = T)
fileNames
#> num1 num2
#> 1 125975 1
#> 2 125975 2
#> 3 126284 1
Created on 2022-05-26 by the reprex package (v2.0.1)
Remove the convert = T
option if you like to keep the values as characters instead of integers.
Hope this helps,
PJ