首页 > 解决方案 > 根据R中多行的条件进行变异

问题描述

如何评估中多行的条件dplyr?我有一个数据集,我想根据多个时间段发生的条件(转换)对其进行变异。

按照下面的示例,如果一个人通过了不良状态,则必须将其整体视为不良。我试过mutate_if但它不起作用,或者我可能无法理解语法

df <-
  data.frame(ID = c(1,1,1,2,2,2,3,3,3),
             Date= c(1,2,3,1,2,3,1,2,3),
             Money = c(500,400,500,100,100,100,200,300,300), 
             Status = c("Good", "Bad", "Good", "Good","Good","Good", "Bad","Good","Good"))

你能为我提供一个解决方案来达到以下结果吗?如果可能的话,我宁愿呆在边界之内dplyr,尽管我知道可以进行一些好的治疗datatable

result <- 
  data.frame(ID = c(1,1,1,2,2,2,3,3,3), 
                     Date= c(1,2,3,1,2,3,1,2,3),
                     Money = c(500,400,500,100,100,100,200,300,300),
                     Status = c("Good", "Bad", "Good", "Good","Good","Good", "Bad","Good","Good"),
                     Status_overall = c("Bad", "Bad", "Bad", "Good","Good","Good", "Bad","Bad","Bad"))

标签: rconditional-statementstransitiondplyr

解决方案


'Bad'如果在 a 中,any Status您可以返回。'Bad'ID

library(dplyr)

df %>%
  group_by(ID) %>%
  mutate(Status_overall = if(any(Status == 'Bad')) 'Bad' else 'Good')
  #Without if/else
  #mutate(Status_overall = c('Good', 'Bad')[any(Status == 'Bad') + 1])

#    ID  Date Money Status Status_overall
#  <dbl> <dbl> <dbl> <chr>  <chr>         
#1     1     1   500 Good   Bad           
#2     1     2   400 Bad    Bad           
#3     1     3   500 Good   Bad           
#4     2     1   100 Good   Good          
#5     2     2   100 Good   Good          
#6     2     3   100 Good   Good          
#7     3     1   200 Bad    Bad           
#8     3     2   300 Good   Bad           
#9     3     3   300 Good   Bad           

这可以用基数 R 写成data.table

df$Status_overall <- with(df, ifelse(ave(Status == 'Bad', ID, FUN = any), 'Bad', 'Good'))

library(data.table)
setDT(df)[, Status_overall := if(any(Status == 'Bad')) 'Bad' else 'Good', ID]

推荐阅读