I am trying to import a series of files from directory and convert each of them into a dataframe. I would also like to use the file title to create two new columns with title-dependent values. Input files have the format: xx_yy.out
Where XX can currently be one of three values. YY currently has two possible values. In the future these numbers will go up.
Edit of Solution based on the comments (see below for the original question)
edited again to reflect suggestions of @JoshO'Brien
filelist <- as.list(dir(pattern = ".*.out"))
for(i in filelist) {
tempdata <- read.table(i) #read the table
filelistshort <- gsub(".out$", "", i) #remove the end of the file
tempsplit <- strsplit(filelistshort, "_") #remove the underscore
xx <- sapply(tempsplit, "[", 1) #get xx
yy <- sapply(tempsplit, "[", 2) #get yy
tempdata$XX <- xx #add XX column
tempdata$YY <- yy #add YY column
assign(gsub(".out","",i), tempdata) # give the dataframe a shortened name
}
Below is the original code showing that I wanted to use some means to ge teh XX and YY values but wasn't sure of the best way:
My outline (after @romanlustrik post ) is as follows:
filelist <- as.list(dir(pattern = ".*.out"))
lapply(filelist, FUN = function(x) {
xx <- grep() or pmatch()
yy <- grep() or pmatch()
x <- data.frame(read.table(x))
x$colx <- xx
x$coly <- yy
return(x)
})
where the xx <-
and yy <-
lines would be a lookup based on either pmatch or grep. I am playing around to make either one work but would welcome any suggestions.