0

I am trying to get some unique combinations of two variables.
For each value of x, I would like to have this unique y value, and drop those have several y values. But several x values could share same y value.
For example,
a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6)),
and I would like to get the output like:

b=data.frame(x=c(2,4,5),y=c(3,3,6))

I have tried unique(), but it does not help this situation. Thank you!

joran
  • 169,992
  • 32
  • 429
  • 468
Lilia Feng
  • 67
  • 4

2 Answers2

1

First we use unique to omit repeated rows with the same x and y values (keeping only one copy of each). Any repeated x values that are left have different y values, so we want to get rid of them. We use the standard way to remove all copies of any duplicated values as in this R-FAQ.

a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))
b = unique(a) 
b = b[!duplicated(b$x) & !duplicated(b$x, fromLast = TRUE), ]
b
#   x y
# 3 2 3
# 4 4 3
# 5 5 6

Fans of dplyr would probably do it like this, producing the same result.

library(dplyr)
a %>% 
  group_by(x) %>%
  filter(n_distinct(y) == 1) %>%
  distinct
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
1

Using dplyr:

library(dplyr)

a <- data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))

a %>% 
  distinct() %>% 
  add_count(x) %>%   # adds an implicit group_by(x)
  filter(n == 1) %>% 
  select(-n)
#> # A tibble: 3 x 2
#> # Groups:   x [3]
#>       x     y
#>   <dbl> <dbl>
#> 1     2     3
#> 2     4     3
#> 3     5     6

Created on 2018-11-14 by the reprex package (v0.2.1)

amanda
  • 321
  • 5
  • 12