0

my first post so please have patience :).

I imported multiple data.frames using the code:

setwd("C:/movie1")
data_list <- list.files(pattern="*.csv")

for (i in 1:length(data_list1)) 
assign(data_list1[i], 
     read.csv(data_list1[i],  
                  sep= ",", 
                  dec = ".",
                  header = T))

which resulted in data.frames named: "well_0.csv", "well_1.csv", etc. (I found this peace of code here, so thank you)

Since I want to import another set of data.frames from a different directory, but with identical names and merge them I would like to change the names of these data.frames to something like "well_0_movie1" or at least "well_0.csv_movie1" so R doesn't overwrite them. Please help

BStat
  • 31
  • 7
  • 2
    Do you need to use `assign`? Why not just read the data.frames into a list using lapply? – Dason Oct 05 '16 at 16:18
  • 2
    Do yourself a big favor [and use a list of data frames instead of a bunch of sequentially named data frames](http://stackoverflow.com/a/24376207/903061). – Gregor Thomas Oct 05 '16 at 16:34
  • @Gregor I was thiking about this but I would have to rewrite my entire code and I don't know if I currently have the time or skill to do this, but tnx for the link. Do you say this because it speeds up the execution (that's what I read) or makes handling data easier? – BStat Oct 05 '16 at 17:27
  • @Dason no special reason for using `assign` (that's the answer i found here). Working hard last couple of days to figure out these functions. I'm actually rewriting my code from loading one file at a time to something a bit more elegant so `assign` seemed like an improvement :P – BStat Oct 05 '16 at 17:32
  • It won't necessarily speed up execution, but it makes your code easier to write and less likely to have bugs. For example, the entire code in your question would be simplified to `setwd("C:/movie1"); data_list = lapply(list.files(pattern = "*.csv"), read.csv, sep = ",", dec = ".", header = T)`. And instead of messing more with `assign` if you want to change the names, you would just use `names()` which you are probably already familiar with. – Gregor Thomas Oct 05 '16 at 18:13
  • But the real benefits are downstream. If you wanted to, say, check the dimension of each of your data frames you can do `sapply(dat_list, dim)` instead of writing a for loop to go over the names of your objects, using `get` to get the actual object and then using `dim` on it, and assigning the result to a preallocated matrix or list. – Gregor Thomas Oct 05 '16 at 18:16
  • @Gregor hmm thank you for the advice. I will try to do this, but data types in R still confuse me sometimes so I'm not sure what functions I can and cannot use – BStat Oct 06 '16 at 13:06

2 Answers2

1

You can remove the .csv from the first time you load your data by using gsub and subsequently use paste add _movie1:

data_list1 <- list.files(pattern="*.csv")
for (i in data_list1) 
  assign(gsub(".csv","",i),
         read.csv(i,header=T))


#Second time (in a different directory)
data_list2 <- list.files(pattern="*.csv")
for (j in data_list2) 
  assign(paste(gsub(".csv","",j),"movie1",sep = "_"),
         read.csv(j,header=T, sep = ",", dec = "."))

I hope this is helpful.

Abdou
  • 10,940
  • 3
  • 27
  • 38
0

You can do

assign(sprintf("%s_movie1",data_list1[i]), read.csv(data_list1[i], sep= ",", dec = ".", header = T))

I can not test it right now, I hope not to have a typographic error.

Roberto Huelga
  • 324
  • 3
  • 10