需要跟踪正在接受,已接受和已放弃化疗的患者。帮助检查化疗方案是否已正确编码。
下面是一些示例数据。一个“拥有”数据集显示了我所拥有的东西,一个“需求”数据集显示了我想要达到的目标。想法是,将基于更改变量的值将药物名称添加到运行清单中/从运行清单中删除。例如,使用“ on”变量,将在change = 1时将药物名称添加到运行列表中,而在change = -1时将药物名称从运行列表中删除。
我的想法是,可以使用purrr包中的accumulate函数或Reduce(accumulate = TRUE)实现此目的。我的理解是,通过重复应用将一对值减少为单个值的函数,这些函数将把较长的列表减少为较短的列表。
我尝试在累加/减少中包括粘贴或str_remove之类的功能(来自stringer包),并且效果很好。问题是,取决于更改的值,有时我想粘贴,有时我想str_remove,有时我想保留列表。我尝试编写一个函数,该函数根据change的值应用不同的函数,但是效果并不理想。
我怎样才能让R做我需要的事情?
谢谢
保罗
#### Have data ####
have <- structure(
list(
PTNO = structure(
c(86319, 86319, 86319, 86319,
86319, 86319, 86319, 86319),
format.sas = "BEST"
),
line = c(1, 1, 1, 1, 1, 1, 1, 1),
change = c(1, 1,-1,-1, 1, 1,-1,-1),
date = structure(
c(16751, 16751, 16764, 16764, 16815, 16815,
16836, 16836),
class = "Date",
format.sas = "MMDDYY"
),
drug = c(
"carboplatin",
"cetuximab",
"carboplatin",
"cetuximab",
"carboplatin",
"cetuximab",
"carboplatin",
"cetuximab"
)
),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-8L))
#### Need data ####
need <- structure(
list(
PTNO = structure(
c(86319, 86319, 86319, 86319,
86319, 86319, 86319, 86319),
format.sas = "BEST"
),
line = c(1, 1, 1, 1, 1, 1, 1, 1),
change = c(1, 1,-1,-1, 1, 1,-1,-1),
date = structure(
c(16751, 16751, 16764, 16764, 16815, 16815, 16836, 16836),
class = "Date",
format.sas = "MMDDYY"
),
drug = c(
"carboplatin",
"cetuximab",
"carboplatin",
"cetuximab",
"carboplatin",
"cetuximab",
"carboplatin",
"cetuximab"
),
on = c(
"carboplatin",
"carboplatin, cetuximab",
"cetuximab",
"",
"carboplatin",
"carboplatin, cetuximab",
"cetuximab",
""
),
taken = c(
"carboplatin",
"carboplatin, cetuximab",
"carboplatin, cetuximab",
"carboplatin, cetuximab",
"carboplatin, cetuximab, carboplatin",
"carboplatin, cetuximab, carboplatin, cetuximab",
"carboplatin, cetuximab, carboplatin, cetuximab",
"carboplatin, cetuximab, carboplatin, cetuximab"
),
dropped = c(
"",
"",
"carboplatin",
"carboplatin, cetuximab",
"carboplatin, cetuximab",
"carboplatin, cetuximab",
"carboplatin, cetuximab, carboplatin",
"carboplatin, cetuximab, carboplatin, cetuximab"
)
),
row.names = c(NA,-8L),
class = c("tbl_df", "tbl", "data.frame"))