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"