3

I have to delete rows from my df in relation to a particular condition.

i have a simple df

sentID   partner  sentence
  A        B        C
  A        B        D
  B        C        E
  B        C        F
  B        A        S

and I want to delete the ones in which sentID & partner are equal and in the same time the sentID is not associated to another partner value. (i.e. The specific sentID does not appear with the same partner value only.) Otherwise, I have to keep the row.

sentID   partner  sentence
  B        C        E
  B        C        F
  B        A        S

In the output example, the rows containing sentID A were deleted because it appears with partner B only. SentID B was kept because it appears with partner C and A.

How could I do?

thank you for your suggestions!

2
  • What do you mean by "not associated" ? Commented Jan 10, 2019 at 12:44
  • @zx8754 The specific sentID does not appear with the same partner value only. In the output example, the rows containing sentID A were deleted because it appears with partner B only. SentID B was kept because it appears with partner C and A. Commented Jan 10, 2019 at 12:47

1 Answer 1

2

Here is a dplyr solution:

df <- data.frame(sentID = c("A", "A", "B", "B", "B"),
                  partner = c("B", "B", "C", "C", "A"))

df
#>   sentID partner
#> 1      A       B
#> 2      A       B
#> 3      B       C
#> 4      B       C
#> 5      B       A

library(dplyr)

df %>% group_by(sentID) %>% filter(length(unique(partner)) > 1)
#> # A tibble: 3 x 2
#> # Groups:   sentID [1]
#>   sentID partner
#>   <fctr>  <fctr>
#> 1      B       C
#> 2      B       C
#> 3      B       A

Created on 2019-01-10 by the reprex package (v0.2.1)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.