I am running expand.grid function. For a simple example,
a <- c(1,2,3,"X","Y","M")
b is identical as b.
if I take expand.grid(a,b), it returns all pairs including diagonal elements — (1,1),(2,2),….(y,y), and also, elements which the first of pairs is larger than the second one –(2,1),(3,2)(x,1),…etc. (but, actually, not numerical comparison here as there are some characters like x,y) I want to exclude such pairs in output. How can I simply generate the output without them by using expand.grid function, Or, any other functions to do it?
A slight modification to Vincent Zoonekynd’s will take care of non-numerical factors:
Basically, what you need is to use string comparison instead of “plain” comparison that doesn’t work on factor variables.