jarciniegas
jarciniegas

Reputation: 47

Filter based on a unique matching condition

I'm trying to execute a command to only keep D where X is the same as it is when Y = "34". I figure a fairly simple filter would do the trick but I've been stuck.

The data looks like this:

 D   X     Y
 1   20    30
 2   22    34
 3   22    34
 4   22    36
 5   24    34
 6   24    34
 7   24    39
 8   28    39

The output I'm looking for then would be this:

 D   X     Y
 2   22    34
 3   22    34
 4   22    36
 5   24    34
 6   24    34
 7   24    39

Thanks!

Upvotes: 2

Views: 50

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101608

A base R option using subset + ave

> subset(df, ave(Y == 34, X, FUN = any))
  D  X  Y
2 2 22 34
3 3 22 34
4 4 22 36
5 5 24 34
6 6 24 34
7 7 24 39

data

> dput(df)
structure(list(D = 1:8, X = c(20L, 22L, 22L, 22L, 24L, 24L, 24L, 
28L), Y = c(30L, 34L, 34L, 36L, 34L, 34L, 39L, 39L)), class = "data.frame", row.names = c(NA, 
-8L))

Upvotes: 1

randr
randr

Reputation: 314

Here is a base R version.

Make the data (normally read in from file):

data = data.frame(D = 1:8, X = c(20, 22, 22, 22, 24, 24, 24, 28), Y = c(34, 34, 36, 34, 34, 39, 39))

And perform the search:

data[which(data$X %in% data$X[data$Y == 34]), ]

This is bundled into one line but basically just finds indices where "X is the same as any value of X where Y is 34". I hope that makes sense!

Upvotes: 1

akrun
akrun

Reputation: 887168

We can use any to check if there is a value of 34 in 'Y' after grouping by 'X' to return those groups

library(dplyr)
df1 %>%
   group_by(X) %>%
   filter(any(Y == 34))

-output

# A tibble: 6 x 3
# Groups:   X [2]
#      D     X     Y
#  <int> <int> <int>
#1     2    22    34
#2     3    22    34
#3     4    22    36
#4     5    24    34
#5     6    24    34
#6     7    24    39

data

df1 <- structure(list(D = 1:8, X = c(20L, 22L, 22L, 22L, 24L, 24L, 24L, 
28L), Y = c(30L, 34L, 34L, 36L, 34L, 34L, 39L, 39L)),
class = "data.frame", row.names = c(NA, 
-8L))

Upvotes: 1

Related Questions