game_of_lies
game_of_lies

Reputation: 55

How to group by then create a new subset in which the values are conditionally based on the each group's first row and a particular value

I have a simple data frame like this

df <- data.frame(x=c(1,1,3,3,2,2,2,1),
                 y=c('a','b','a','b','e','a','d','c'))

enter image description here

I want to group by x, create a new data frame of 2 columns: 'x' and 'test'. The value of 'test' will be based on conditions:

So the expected outcome would be as below

enter image description here

Thank you very much.

Upvotes: 0

Views: 121

Answers (2)

det
det

Reputation: 5232

df %>%
  group_by(x) %>%
  summarise(test = (first(y) == "a" && any(y == "c") || (first(y) == "e" && any(y == "d"))) * 1L)

Upvotes: 2

Phil
Phil

Reputation: 8127

library(dplyr)
library(stringr)

df |> 
  group_by(x) |> 
  mutate(test = (row_number() == 1 & y == "a" & sum(str_detect(y, "c"))) | 
           (row_number() == 1 & y == "e" & sum(str_detect(y, "d")))) |> 
  summarize(test = sum(test))

Upvotes: 1

Related Questions