I'm trying to combine multiple CSV files in R and have come across an issue I can't solve even though I've searched this website.
I'm using a function to read multiple CSV files and merge them using the reduce command.
This isn't working as I get the following error:
Error in match.names(clabs, names(xi)) :
names do not match previous names
So I took 4 CSVs manually out of the bunch I want to run to see if I could solve this issue and here is the code for these 4:
dataframeA=read.csv("1.csv")
dataframeA <- subset(dataframeA, select = c(Name,Position))
dataframeB=read.csv("2.csv")
dataframeB <- subset(dataframeB, select = c(Name,Position))
dataframeC=read.csv("3.csv")
dataframeC <- subset(dataframeC, select = c(Name,Position))
dataframeD=read.csv("4.csv")
dataframeD <- subset(dataframeD, select = c(Name,Position))
prac=Reduce(function(x,y) merge(x,y,by="Name",all=TRUE), list(dataframeA, dataframeB, dataframeC, dataframeD))
Where I have taken out two particular columns of each of my CSV files (there are 3 in total but the 3rd column is to be used later on), there was also a need to perform an outer join.
When running this code I get the same error as before so I looked up the cause of this and found as the error suggests the column names aren't the same. I produced queries between these 4 data frames to see:
> identical(colnames(dataframeA),colnames(dataframeB))
[1] TRUE
> identical(colnames(dataframeA),colnames(dataframeC))
[1] TRUE
> identical(colnames(dataframeA),colnames(dataframeD))
[1] TRUE
As you can see this doesn't bring back any issues for my column names being different so I am at a loss about how to fix this.
Also what is interesting is running the same command with only three data frames (yes it can be any of the three data frames) I get no error whatsoever for example running the following gives no error at all, I can also run this for dataframeD instead of any of the other data frames and also get it to run.
prac=Reduce(function(x,y) merge(x,y,by="Name",all=TRUE), list(dataframeA, dataframeB, dataframeC))
I may have just overlooked something very simple but I cannot find the error at all.
Thanks for any help in advance
Nathan