0

I prepared a simple code for my question due to the original data volume is huge.

df <- data.frame(X=c(0,0,1,1,1,1),Y=c(0,0,0,0,1,1),Z=c(1.5,2,5,0.7,3.5,4.2))

I'm trying to extract all actually existing combinations in columns X and Y. So the expected result will be (0,0),(1,0),(1,1).

But, If I utilize expand.grid, it will return all available combinations mathematically with elements 0 & 1. So (0,1) will be included in the result

So my question is how to extract only actually existing combinations in two different columns? Any opinion is welcome!

1 Answers1

0

We can subset the relevant columns and then use unique over it.

unique(df[c('X', 'Y')])

#  X Y
#1 0 0
#3 1 0
#5 1 1

Or in dplyr, use distinct

library(dplyr)
df %>% distinct(X, Y)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213