[R] question regarding intersect function

David Winsemius dwinsemius at comcast.net
Tue Oct 25 16:07:14 CEST 2011


On Oct 25, 2011, at 6:07 AM, Samir Benzerfa wrote:

> Hi
>
>
>
> I have probably a very simple question but I'm going crazy trying to  
> find
> the solution.
>
>
>
> I have two data.frames with headers and I'm doing an intersection  
> between
> them by names, such that the intersected data.frames are returned by:
> df1[intersect(names (df1), names(df2))] and the same for df2
>
>
>
> Now, I want to have all the opposite data that did not intersect. I  
> tried to
> do: df1[-intersect(names (df1), names(df2))]  and some other stuff  
> but it
> didn't work.

Right. You cannot use negative indexing with character column  
identifiers.

Two methods you can try:
df1[ setdiff( names(df1), names(df2)) ]
df2[ setdiff( names(df2), names(df1)) ]

Or:
df1[!names(df1) %in% names(df2)]

(They both use match() "under the hood" so there would also be a  
match() based solution as well.)

--

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list