r - 根据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"))
解决方案
'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]
推荐阅读
- angular - 模板引用变量:分配 ngForm 和 ngModel
- android - 通过 ACTION_VIEW 意图将图像从内部存储保存到图库
- c - fdim 的首字母缩略词代表什么?
- javascript - javascript如何将“空”插入数组?
- android-studio - 尝试在 Android Studio 3.5.3 中创建新项目时出现很多错误
- vb.net - 在文件夹日期上使用获取文本后,如何获得正确的日期格式?
- javascript - 切换数组值的最快方法
- node.js - 如何在谷歌云功能中正确路由所有 nodejs express 应用程序请求
- java - System.out.println("Serializable:" + arrayList instanceof Serializable) 没有打印 'Serializable' 字
- wordpress - 在 WooCommerce 订单中创建自定义元信息