Home > Enterprise >  Filter according to two conditions and a category in R
Filter according to two conditions and a category in R

Time:03-11

What I intend to do is from a database, filter the rows according to two conditions, and then extract its other row with its same category.

I show you my database.

    dat <- read.table(text="
Category  Score   Index    Document
Pepe        5      Al           a
Pepe        1      Bl           b
Juan        8      Cl           a
Juan        5      Al           b
Mikel       1      Cl           a
Mikel       6      Bl           b
", header=TRUE

My intention is then to obtain the rows that meet two conditions. Index = Bl and Document=b. But not only get its rows, but also get the rows with the same category even if it does not meet the previous condition.

That is, I want it to return this:

enter image description here

Can you help me, please?

CodePudding user response:

Couple of options :

Base R -

subset(dat, Category %in% Category[Index == "Bl" & Document == "b"])

#  Category Score Index Document
#1     Pepe     5    Al        a
#2     Pepe     1    Bl        b
#5    Mikel     1    Cl        a
#6    Mikel     6    Bl        b

dplyr -

library(dplyr)

dat %>%
  group_by(Category) %>%
  filter(any(Index == "Bl" & Document == "b")) %>%
  ungroup()

CodePudding user response:

One approach would be left_join with itself after filtering.

library(dplyr)

dat |>
  filter(Index == "Bl", Document == "b") |>
  select(Category) |>
  left_join(dat)

  Joining, by = "Category"
  Category Score Index Document
1     Pepe     5    Al        a
2     Pepe     1    Bl        b
3    Mikel     1    Cl        a
4    Mikel     6    Bl        b

  • Related