1

Is there a set of R functions analogous to intersect, union, etc. which act on vectors of coordinates? Take a two collections of locations in N dimensions, each of which could be viewed as N-row by M-location array, and find the common locations (i.e. columns), and so on. I suppose this could be done with a couple *apply lines to compare every column in one array to the columns of the other, but was hoping for something faster or neater.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73

1 Answers1

3

There is a 'sets' package, but the simplest solution would be to use the base set operations: union, , intersect , and setdiff on the results of paste with a separator such as "_". If these are represented as 'numeric" values you may need to make decsions about what really means "equality" and for that some round-ing or trunc-cation may be needed.

Another thought is that implementation might be more self-documenting in the columnwise arrangement you describe to use the "plyr" function:colwise. I'm not a dedicated user of plyr methods, but this appears to be how it might start:

colpastefn <- colwise( function(...){paste(... , sep="_") )

Although the description of the function suggests it should work as a standalone function on a dataframe, all of the illustrated uses are with ddply.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Works for me. I certainly agree that an intelligent experimentalist :-) would want to `trunc` his values before deciding which locations were identical. I'll check this answer unless someone comes up with something spectacular in the near future. – Carl Witthoft Mar 15 '12 at 12:34