newror
newror

Reputation: 53

How can I inner join two csv files in R?

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.

Upvotes: 5

Views: 3352

Answers (2)

John
John

Reputation: 23758

This is a direct way to do it by doing %in%. This will be about the fastest way entirely within R.

read in the files

datf1 <- read.table("file1.csv") #two column file
datf2 <- read.table("file2.csv") #one column file

select the rows you want... %in% makes a logical vector that is the length of the first argument and is TRUE when an item is in both of the arguments and FALSE otherwise.

datf1 <- datf1[datf1[,2] %in% datf2[,1],]

write it out... I changed the file name from file1 because you really shouldn't overwrite your original data and make a new file1.

write.table(datf1, "file3.csv", sep = ',', row.names = FALSE, quote = FALSE)

Upvotes: 0

Anatoliy
Anatoliy

Reputation: 1380

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")

Upvotes: 7

Related Questions