StevenL StevenL - 1 month ago 8
R Question

extracting varible from file names in R

I have files that contain multiple rows, I want to add two new rows that I create by extracting varibles from the filename and multipling them by current rows.
For example I have a bunch of file that are named something like this

file1[1000,1001].txt

file1[2000,1001].txt


between the
[]
there are always 2 numbers spearated by a comma

the file itself has multiple columns, for example
column1
&
column2


I want for each file to extract the 2 values in the name of the file and then use them as variables to make 2 new columns that used the variable to modify the values.

for example

file1[1000,2000]


the file contains two columns

column1 column2
1 2
2 4


I want at the end to add the first file name value to column 1 to create column3 and add the second file name value to column 2 to create column 4, ending up with something like this

column1 column2 column3 column4
1 2 1001 2002
2 4 1002 2004


thanks for the helpalmost there just a few more issues
original files has 2 columns "X_Parameter" "Y_Parameter", the file name is "test(64084,4224).txt
your code works great at extracting the two values V1 "64084" and V2 "4224" from the file name. I then add these values to the origninal dataset. this yields 4 columns. "X_Parameter" "Y_Parameter" "V1" "V2".

setwd("~/Desktop/txt/")
txt_names = list.files(pattern = ".txt")
for (i in 1:length(txt_names)){assign(txt_names[i], read.delim(txt_names[i]))
DS1 <- read.delim(file = txt_names[i], header = TRUE, stringsAsFactors = TRUE)
require(stringr)
remove_text <- str_extract(txt_names, pattern = "\\[[0-9,0-9]+\\]")
step1 <- gsub("(\\[)", "", remove_text)
step2 <- gsub("(\\])", "", step1)
DS2<-as.data.frame(do.call("rbind", (str_split(step2, ","))))
DS1$V1<-DS2$V1
DS1$V2<-DS2$V2


My issue arises when tying to sum "X_Parameter" and "V1" to make "absoluteX" and sum "Y_Parameter"with "V2" to make "absoluteY" for each row.

below are the two ways I have tried with the errors

DS1$absoluteX<-DS1$X_Parameter+DS1$V1


error
In Ops.factor(DS1$X_Parameter, DS1$V1) : ‘+’ not meaningful for factors

other try was

DS1$absoluteX<-rowSums(DS1[,c(“X_Parameter”,”V1”)])


error
Error in rowSums(DS1[, c("X_Parameter", "V1")]) : 'x' must be numeric

Any thoughts?Thanks

Answer
## set path to wherever your files are
setwd("path")

## make a vector with names of your files
txt_names <- list.files(pattern = ".txt")

## read your files in
for (i in 1:length(txt_names)) assign(txt_names[i], read.csv(txt_names[i], sep = "whatever your separator is"))

## grab the text you require from the file names
require(stringr)

remove_text <- str_extract(txt_names, pattern = "\\[[0-9,0-9]+\\]")
step1 <- gsub("(\\[)", "", remove_text)
step2 <- gsub("(\\])", "", step1)

## step2 should look like this
> step2

[1] "1000,1001"

## split each string and convert to data frame with two columns 
as.data.frame(do.call("rbind", (str_split(step2, ","))))

NOw you have a data.frame with two columns , each containing the file name parts you need. Just rep them times length of each of your data.frames and cbind.