简体   繁体   中英

Using ifelse to recode across multiple rows within groups

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)

初步结果

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

So I want it to look like:

最后结果

I tried group_by, but that was not effective.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM