0

I have a lot of csv files that I want to read and then merge with a larger file (each file merged individually). So I wrote this function to read the files (works):

read <- function(x) {
    read.csv(paste("StringCount_Length=", x, ".csv", sep = ""), header=TRUE, sep=",")
}

Now I want to loop through the files, read and merge them. However, the merge does not work giving me this error message:

Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

I do not get the error if I put a specific file in the merge command so my mistake must be there but I can't quite see where I went wrong. Would be grateful for any help or advice!

for (x in c(2:5)) { 
  assign(paste("data", x, sep=""), read(x))
  assign(paste("data_total_",x, sep=""), merge(paste("data", x, sep=""),    data_old, by="Subject"))
}
MrLeeh
  • 5,321
  • 6
  • 33
  • 51
M.Stadler
  • 1
  • 1
  • Typo in the variable name maybe? Are you certain the variable `Subject` is present in every dataset? – LAP May 17 '18 at 09:14
  • That was my first thought as well, but it seems to be all right. And it works fine when I replace the paste statement with a data file – M.Stadler May 17 '18 at 09:21
  • Also, you may want to consider using a list of data.tables, and combine them using Reduce, as introduced in this [answer](https://stackoverflow.com/a/13274291/4182611). – tyumru May 17 '18 at 10:53

1 Answers1

1

Similar to the assign function where you are assigning an object to a name string, you need to use the get function where you invoke an object using a name string of the object instead of directly using the object name.

The code below should do the job where we encapsulate the paste function with the get function.

e.g. For x = 2, data2 data frame will be accessed with get("data2") function call

for (x in c(2:5)) { 
  assign(paste("data", x, sep=""), read(x))
  assign(paste("data_total_",x, sep=""), 
  merge(get(paste("data", x, sep="")),    data_old, by="Subject"))
    }
serge.karalenka
  • 980
  • 1
  • 15
  • 29