Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inner join two csv files in R?

Tags:

r

csv

I have two csv files.

File one has two columns:

DD1 abct
DD2 geate
DD3 watec
DD4 wwwca21
DD5 bate789

File two has one column:

abct
geate
bate789

I want to get a truncated file one to include those that matched with file two, i.e.

DD1 abct
DD2 geate
DD5 bate789

Could you mind to let me know how to do it with R?

New to R.

like image 941
newror Avatar asked Aug 25 '11 08:08

newror


People also ask

How do I join a CSV file in pandas?

To merge all CSV files, use the GLOB module. The os. path. join() method is used inside the concat() to merge the CSV files together.


1 Answers

First, read the files with the read.table:

file1 <- read.table("file1.csv", col.names=c("FOO", "BAR"))
file2 <- read.table("file2.csv", col.names=c("BAR"))

Then merge them:

merged <- merge(file1, file2)

And write the result:

write.table(merged, "merged.csv")
like image 90
Anatoliy Avatar answered Sep 17 '22 06:09

Anatoliy