[英]Updating values in a column based on criteria
I need to update x_eligible to 1, for all corresponding records of same study_id & site, where x == 1. This is to sort of to overwrite the values of x for the same records as study_id & site, whenever x_eligible is set to 1 对于x == 1的同一个study_id&site的所有相应记录,我需要将x_eligible更新为1,这是为了在x_eligible设置为1时覆盖与study_id&site相同的记录的x值。
Here is a reproducible code: 这是可复制的代码:
library(tidyverse)
newdata = data.frame(site = c('A','A','A','B','B','B','B'),
study_id = c(1,1,2,1,1,1,2),
x = c(0,1,0,0,NA,1,0),
x_eligible = c(0,1,0,0,0,1,0))
xEligibility2 <- function (x, siteid, studyID){
el = newdata %>% filter(site == siteid & study_id ==studyID & x_eligible==1)
if(exists("el"))
return(ifelse(nrow(el)>=1,1,0))
else
return(0)
}
newdata = newdata %>% mutate(
x_eligible = ifelse(apply(newdata, 1, xEligibility2, siteid=site, studyID=study_id) == 1, 1, 0)
)
And this is what I am getting as results on the x_eligible column: 这就是我在x_eligible列上得到的结果:
x_eligible = c(1,1,1,1,1,1,1)
x_eligible is all being set to 1. x_eligible全部设置为1。
This is my expected output: 这是我的预期输出:
x_eligible = c(1,1,0,1,1,1,0)
I would appreciate any help to point out what I could be doing wrong. 我将不胜感激,指出我可能做错了什么。
You can group by site and study_id and test if any of the values in x equal 1: 您可以按site和study_id进行分组,并测试x中的任何值是否等于1:
library(dplyr)
newdata %>%
group_by(site, study_id) %>%
mutate(x_eligible = +any(x == 1))
# A tibble: 7 x 4
# Groups: site, study_id [4]
site study_id x x_eligible
<fct> <dbl> <dbl> <int>
1 A 1 0 1
2 A 1 1 1
3 A 2 0 0
4 B 1 0 1
5 B 1 NA 1
6 B 1 1 1
7 B 2 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.