Silvia
Silvia

Reputation: 405

How to delete rows under a specific condition in R?

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!

Upvotes: 3

Views: 91

Answers (1)

gfgm
gfgm

Reputation: 3647

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)

Upvotes: 2

Related Questions