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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to