[R] Intersect 2 lists+bring extra columns

jsteimle jsteimle at uchicago.edu
Sun Sep 1 22:18:50 CEST 2013


Hi everyone,

I am pretty new to R, so be patient. 

I am trying to intersect 2 columns and in the rows that intersect, I want
information from the 3rd column to be brought with it. I think it will be
easier to explain with an example  example.csv
<http://r.789695.n4.nabble.com/file/n4675136/example.csv>  . 

In my example, I have a reference list of fruit (first column), and my fruit
of interest (second column), and then in the third column, I have color
information about the fruit of interest in the second column. 

Currently to find the intersection between column 1 and 2, I use 
>fruit<-read.csv("//Users//J//Desktop//example.csv", header=TRUE)
>output<-intersect(fruit[,1],fruit[,2])
>write.table(data.frame(fruit),"output.xls", col.names=TRUE,
row.names=FALSE)

However, it would save me a lot of time if I could have the color
information from column 3 be saved with the overlap. I normally have
reference list of several hundred and lists of interest in the several
thousand, and bringing over the information column would be hard manually.

Is there some sort of If function I could be using? I would really like
something like If row x, column 2 intersects with row x, column 1, then row
x column 3 is stored with it. I can think through the logic, but not sure
how to do it in R. 

Any help would be much appreciated!



--
View this message in context: http://r.789695.n4.nabble.com/Intersect-2-lists-bring-extra-columns-tp4675136.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list