0
votes

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!

1
build the statement as a string and then use eval(stmt) where stmt is the string you've created - johnjps111
Is there any reason why you need to put those data in the global environment? Having them in a list would be safer and easier for manipulation. - Kota Mori
No need to have them all in the global environment, the only reason I have done so is I found code that allowed me to pull them all in. Do you have an alternative for having them in a list? Thank you. - user2716568
so the names of dataframe are based on the filename? - joel.wilson
Yes, the names of the dataframe are based on the filename. - user2716568

1 Answers

1
votes

might this work ! I actually needed more clarifications on the data and naming to be followed. So let me know if you have any questions

path = "D:/pathname/"
l = list.files(path, pattern = ".csv")
# below func does importing and creation of new columns
func <- function(i){
  df <- read.csv(paste0(path,l[i]))
  names <- unlist(strsplit(l[i], "_"))
  df["BodyPart"] <- names[1]
  df["Name"] <- names[2]
  return(df)
}
# l1 shall have each of the dataframes individually with new columns attached
l1 = lapply(1:length(l), func)
# here we combine all dataframes together
l2 <- as.data.frame(l1)