**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
4天前关闭。
Improve this question
当参与者在调查日期前5年或不到5年被诊断患有该疾病时,我想将该参与者标记为“具有感兴趣的结果”。
所以我想指定:疾病=是&诊断年龄=与调查时的年龄相比〈= 5岁。
我想一定是这样的,但我在网上找不到答案。
data <- mutate(variable_x = case_when(variable_a == 1 & (variable_b == - <=5 years than variable_c) ~ disease, variable_a == 0 ~ disease))
variable_x is the new variable
variable_a is the old variable (simply disease 'yes or no')
variable_b is the age when diagnosed with disease
variable_c is age at time of the survey
我在网上找不到任何关于如何做到这一点的东西。有人能帮我吗?
2条答案
按热度按时间aydmsdu91#
在发布查询时创建minimal reproducible example总是很好的。
不确定你的数据是什么样子的,但是假设你有一个这样的数据框:
然后可以使用
mutate
和case_when
添加所需的列bqucvtff2#
它非常简单,你甚至不需要使用mutate或任何复杂的东西。
假设您有一个名为
df
的data.frame,其中列名为disease
和age
。然后,您可以使用以下命令创建新列:
ofinterest
将是一个新列,当满足条件时,值为TRUE
,当不满足条件时,值为FALSE
。