Reputation: 1
Friends, I got the following code for combining multiple csv files in a particular folder, the person who helped me in trying this code, for him its works well... but when I use it, it is not showing any errors but in the output I m getting only the headers and not the data.
can you please suggest me how to overcome this please??? the code would be like the following,
setwd("C:/Users/dsuresh/Desktop/io")
multmerge = function(mypath){
filenames=list.files(path=mypath, full.names=TRUE)
datalist = lapply(filenames, function(x){read.csv(file=x,header=T)})
Reduce(function(x,y) {merge(x,y)}, datalist)
}
mydata=multmerge("C:/Users/dsuresh/Desktop/io")
View(mydata)
thanks for your time friends...
Upvotes: 0
Views: 456
Reputation: 35
rbind
works perfectly in your case.
csvFile1 = "col1,col2,col3\na1,a2,a3\nb1,b2,b3"
csvFile2 = "col1,col2,col3\nc1,c2,c3\nd1,d2,d3"
csvFile3 = "col1,col2,col3\ne1,e2,e3\nf1,f2,f3"
con1 = textConnection(csvFile1)
con2 = textConnection(csvFile2)
con3 = textConnection(csvFile3)
csvList = list(con1, con2, con3)
multmerge = function(files){
datalist = lapply(files, read.csv)
Reduce(rbind, datalist)
}
mydata=multmerge(csvList)
View(mydata)
Moreover, you could do just do.call(rbind, datalist)
instead of Reduce
And you have no need to specify header=T
in read.csv
- it is default option.
Upvotes: 0
Reputation: 5555
I think your file is not proper .csv (comma seperated values file). try replacing read.csv
function with read.table
and check. Otherwise there is no problem in this function.If doesnt work, try using rbind
instead of merge
. merge function matches two columns from data and then merges. If your data doesn't have identical rownames or columns it won't work properly.
Upvotes: 1