-1

In a folder, I have .csv files named covariates4.csv, covariates11.csv, covariates28.csv, ... (180 files with non-consecutive numbers)

which I would like to merge with files named: species4.csv, species11.csv, species28.csv, ... (same numbers as the covariates files above)

I would like to create a for loop that merges the covariates4.csv with species4.csv by column "X" into a new .csv file called newfile4.csv and do this for each paired files in my folder. Still being a novice in R, I am having trouble writing my for loop to read the different paired numbers... any help would be greatly appreciated!

This is what I have tried so far:

for (i in 1:6550){
covar[i]<-read.csv("covariates[i].csv")
species[i]<-read.csv("species[i].csv")
newfile[i]<-merge(covar[i], species[i], by="X")
write.csv(newfile[i], file="newfile[i].csv")
}

But I get an error message: "cannot open file 'covariates[i].csv': No such file or directory"

TBC
  • 5
  • 3
  • 2
    Still unclear. What is "I am having trouble writing my for loop to read the different paired numbers" ? The loop would be `for (x in c(4, 11, 28, ....))` . Is that what you mean? – user31264 Dec 19 '16 at 17:29
  • 1
    Welcome to SO! I urge you to quickly read about [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [minimal examples](http://stackoverflow.com/help/mcve), both explaining how best to ask a question to maximize likelihood of a fast and usable answer. Up-front: show your code and sample data, two huge points. (Please do scan the links, though, it's fast, easy, and helps us help you!) – r2evans Dec 19 '16 at 17:31
  • Sorry, I don't know how to write the loop that tells R to match the file covariates4.csv with species4.csv and do that for all the pairs in my folder – TBC Dec 19 '16 at 17:31
  • Explain what did you do already. – user31264 Dec 19 '16 at 17:34
  • I don't know how to write a loop that matches covariates4.csv with species4.csv and do that for each pair in folder – TBC Dec 19 '16 at 17:40
  • Two components of your task are (1) matching and (2) loop over all pairs. It seems from the comment that you just want us to write the program instead of you. – user31264 Dec 19 '16 at 17:44
  • for (i in 1:10){ covar[i]<-read.csv("covariates[i].csv") rub[i]<-read.csv("rubriventer[i].csv") rubocc[i]<-merge(covar, rub, by="X") write.csv(rubocc[i]) } – TBC Dec 19 '16 at 17:50
  • I get an error message that says "cannot open file 'covariates[i].csv': No such file or directory" .. is there a way to indicate to do this for the file ending in the number 4, 11, 28, etc.? – TBC Dec 19 '16 at 17:55

1 Answers1

0

If you let i equal a vector of the numbers used to identify your files, you can use paste0 to write out the correct file name during each iteration.

for(i in c(4, 11, 28)){
  covar.i <- read.csv(paste0("covariates", i, ".csv"))
  rub.i <- read.csv(paste0("rubriventer", i, ".csv"))
  rubocc.i <- merge(covar.i, rub.i, by = "X")
  write.csv(rubocc.i, file = paste0("output", i, ".csv"))
}
Thom Quinn
  • 308
  • 1
  • 7