0

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.

1 Answer 1

1

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
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.