# how to combine multiple csv files in R using a common column in all the files?

#### eswari

##### New Member
Hello Hello Everyone,
please help me to write a code in combining multiple csv files in R using a common column in all the files( but diff no.of rows like 702, 666, etc., ) and for missing values I need NA in the place of missing values.
""A COL"" IS THE
COMMON field IN ALL THE FILE.

EACH COLOR in the attached file IS A DIFFERENT CSV FILES SEPARATELY, HERE I COMBINED MANUALLY BASED ON THE field name. IN EACH FILE THE field name WILL BE THERE. BUT IN OUTPUT IF IT APPEARS ONCE ITS ENOUGH FOR ME. with the filename as its header along with its col name.

LIKE THIS I NEED NA FOR MISSING VALUES BASED ON THE name field.
WHICH MEANS IN THIS FILE THERE IS NO VALUE AGAINST THIS RESPECTIVE FIRST COLUMN VALUES -SO NA THERE.

for your easy understanding, here I m showing how individual file looks since here I cannot attach multiple files. I am just pasting one.

name projected_leaf_area treatment species g_alias replicates
1 A-2:1 215.209 WW Chickpea ICCRIL03-0013 2
2 A-2:2 148.404 WW Chickpea ICCRIL03-0119 2
3 A-2:3 206.566 WW Chickpea ICCRIL03-0007 2
4 A-2:4 189.827 WW Chickpea ICCRIL03-0161 2
5 A-2:5 208.204 WW Chickpea ICCRIL03-0033 2
6 A-2:6 146.876 WW Chickpea ICCRIL03-0120 2
7 A-2:7 188.301 WW Chickpea ICCRIL03-0088 2
8 A-2:8 183.169 WW Chickpea ICCRIL03-0145 2
9 A-2:9 165.892 WW Chickpea ICCRIL03-0203 2
10 A-2:10 163.7 WW Chickpea ICCRIL03-0112 2
. .. . . . ... ........ ......
. .. . . . ... ........ ......
702 B-2:234 242.064 WW Chickpea ICCRIL03-0143 4
this total no.of rows varies from sheet to sheet thus the missing values are required for me in the output files..

for easy unserstanding above I have colored each column . for the first column no heading
I hope I explained clearly...

looking forward for help please..

thank you for your time.
pls find the attachments.

Last edited:

#### trinker

##### ggplot2orBust
I have used something like the following in the past:

Code:
Reduce(function(...) merge(..., all=T), list.of.data.frames)

#### eswari

##### New Member
i have used something like the following in the past:

Code:
reduce(function(...) merge(..., all=t), list.of.data.frames)
thank you for your valubale time trinker and helping me...
I used your command but its ia saying as list of data frames object not found...
How to overcome this???

Thank you and looking forward for your reply...

#### Lazar

##### Phineas Packard
Ok so what was the exact code you used?

#### eswari

##### New Member
Ok so what was the exact code you used?
Helloo Hello Lazar,
thanks for coming forward to help me and thanks for your time.. I m new to r programming environment so I couldn't do it ....
the code I used would be like this..

Code:
 setwd("C:/Users/dsuresh/Desktop/SUN")
Reduce(function[B](chickpea12-03-2014.csv, chickpea12-04-2014.csv, chickpea12-05-2014.csv, chickpea12-06-2014.csv)[/B] merge[B](chickpea12-03-2014.csv, chickpea12-04-2014.csv, chickpea12-05-2014.csv, chickpea12-06-2014.csv, [/B]all=T), list.of.data.frames)

chickpea12-03-2014.csv, chickpea12-04-2014.csv, chickpea12-05-2014.csv, chickpea12-06-2014.csv would be my file names in that sun folder...

looking forward for your reply...
for your easy understanding pls see the attached file above in the first question for your kind reference and perusal...
thank you...

#### eswari

##### New Member
thanks all for your valuable time friends.. I got the command the how to merge..
thanks a lot... let me close this thread...

but how to close this thread... ??? where is that option????

#### netdbaind

##### New Member
Hi Eswari,

Can you please let me know what you did for this ? Appreciate your help here Thanks.

#### trinker

##### ggplot2orBust
I missed this thread continuation (lesson on why it's important to respond to a contributor ASAP as your thread can get buried). eswari needed to read the csv files the the Reduce command in first using something like:

Code:
list.of.data.frames <- lapply(dir()[tools::file_ext(dir()) == "csv"], read.csv)
Reduce(function(...) merge(..., all=T), list.of.data.frames)
On top of that it was Reduce not reduce.