I have > 50 .csv files in a folder on my computer. The files all contain the same column headings/ format.
I have code to import all the .csv files and name them appropriately:
path <- "~/My folder Location/"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
perpos <- which(strsplit(file, "")[[1]]==".")
assign(
gsub(" ","",substr(file, 1, perpos-1)),
read.csv(paste(path,file,sep="")))
}
I now have many .csv files that are named, as I prefer, in the environment. However, I now wish to create two columns within each data.frame based on parts of the data.frame name and then create one big data.frame
For example, if one of the data.frames is:
LeftArm_Beatrice
I wish to include:
LeftArm_Beatrice$BodyPart <- c("LeftArm")
LeftArm_Beatrice$Name <- c("Beatrice")
Another example, if one of the data.frames is:
RightLeg_Sally
I wish to include:
RightLeg_Sally$BodyPart <- c("RightLeg")
RightLeg_Sally$Name <- c("Sally")
I then want to merge all these 50+ data.frames into one. If these steps can be included in my importing code, that would be fantastic.
Thanks!
eval(stmt)where stmt is the string you've created - johnjps111