Home > Enterprise >  Using ifelse to recode across multiple rows within groups
Using ifelse to recode across multiple rows within groups

Time:06-29

I need to create a new column based on a pre-existing one and I need that value to be created across all rows of the episode.

episode_id <- c(2,2,56,56,67,67,67)
issue <- c("loose","faulty","broke","faulty","loose","broke","missing")
df <- data.frame(episode_id,issue)

Using ifelse, I can create a new column called "broke" which accurately indicates whether the issue had "bro" in it for each row.

df$broke <- ifelse(grepl("bro",df$issue),1,0)

Initial Result

However, I want it to indicate a "1" for every row with the same episode_id.

So I want it to look like:

Final Result

I tried group_by, but that was not effective.

CodePudding user response:

group_by is the beginning and you can continue with a mutate() and a any() to convert the presence of broke in each piece to at least one in the group:

library(dplyr)
df %>% 
  group_by(episode_id) %>% 
  mutate(broke = as.numeric(any(grepl("bro", issue)))) %>% 
  ungroup()
# A tibble: 7 × 3
  episode_id issue   broke
       <dbl> <chr>   <dbl>
1          2 loose       0
2          2 faulty      0
3         56 broke       1
4         56 faulty      1
5         67 loose       1
6         67 broke       1
7         67 missing     1
  • Related