我有一个药物数据集,其中包含每位患者及其所用药物的信息:
Record.ID Label.Name Generic.Medication.Name Strength Quantity Days.Supplied Date.of.Fill GCN GC3 NDC category
4 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 30000 30 2014-06-18 19154 M4D 00310075290 statins
5 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 90000 90 2014-06-25 19154 M4D 00310075290 statins
6 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 90000 90 2014-09-30 19154 M4D 00310075290 statins
7 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 90000 90 2014-12-18 19154 M4D 00310075290 statins
8 aaaaa CRESTOR TAB 10 MG ROSUVASTATIN CALCIUM TAB 10 MG 10 MG 90000 90 2014-12-18 19153 M4D 00310075190 statins
60 bbbbb TELMISARTAN TAB 20 MG TELMISARTAN TAB 20 MG 20 MG 90000 90 2014-01-24 23833 A4F 00054054218 RASA
61 bbbbb TELMISARTAN TAB 20 MG TELMISARTAN TAB 20 MG 20 MG 90000 90 2014-04-03 23833 A4F 00054054218 RASA
62 bbbbb TELMISARTAN TAB 20 MG TELMISARTAN TAB 20 MG 20 MG 90000 90 2014-07-21 23833 A4F 00054054218 RASA
63 bbbbb TELMISARTAN TAB 20 MG TELMISARTAN TAB 20 MG 20 MG 90000 90 2014-10-22 23833 A4F 00054054218 RASA
66 ccccc ENALAPRIL MALEATE TAB 2.5 MG ENALAPRIL MALEATE TAB 2.5 MG 2.5 MG 15000 30 2014-01-06 963 A4D 00378105101 RASA
我有一个叫做calc_adherence< - function(fill,year)的函数,它输入一个数据集并返回一行: 例如,
fill <-
Record.ID Label.Name Generic.Medication.Name Strength Quantity Days.Supplied Date.of.Fill GCN GC3 NDC category
4 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 30000 30 2014-06-18 19154 M4D 00310075290 statins
5 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 90000 90 2014-06-25 19154 M4D 00310075290 statins
6 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 90000 90 2014-09-30 19154 M4D 00310075290 statins
7 aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG 90000 90 2014-12-18 19154 M4D 00310075290 statins
8 aaaaa CRESTOR TAB 10 MG ROSUVASTATIN CALCIUM TAB 10 MG 10 MG 90000 90 2014-12-18 19153 M4D 00310075190 statins
函数返回
Record.ID Label.Name Generic.Medication.Name Strength Category First_fill Last_fill Duration DaysCovered Year Method Adherence
aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG statins 2014-06-18 2014-12-18 197 days 197 2014 PDC 1
我的问题是,现在我想首先按Record.ID和类别对药物数据集进行分组,然后将calc_adherence应用于每个小组,这样我就可以得到每个患者的结果以及他们服用的每种药物。 我试试
ddply(category.medication, c('Record.ID','category'), summarize, function(x) calc_adherence(x, year)),
但这不起作用.. 我想要的最终数据集是
Record.ID Label.Name Generic.Medication.Name Strength Category First_fill Last_fill Duration DaysCovered Year Method Adherence
aaaaa CRESTOR TAB 20 MG ROSUVASTATIN CALCIUM TAB 20 MG 20 MG statins 2014-06-18 2014-12-18 197 days 197 2014 PDC 1
aaaaa ... RASA ... PDC 0.8
bbbbb ... RASA ... PDC 0.75
答案 0 :(得分:0)
尝试
aggreagate
例如,如果您的数据被命名为&#34;数据&#34;
aggregate(data, by = list(data$Record.ID), calc_adherence)