假设我有一个如下所示的数据框(df):
Patient VisitDate Test
1 1mth A
1 2mth A
1 3mth A
1 4mth A
2 1mth B
2 2mth B
2 3mth B
2 4mth B
3 1mth A
3 2mth A
3 3mth B
3 4mth A
以下是制作示例数据框的代码:
df <- data.frame(Patient=c(1,1,1,1,2,2,2,2,3,3,3,3),
VisitDate=c("1mth","2mth","3mth","4mth","1mth","2mth","3mth","4mth","1mth","2mth","3mth","4mth"),
Test=c("A","A","A","A","B","B","B","B","A","A","B","A"))
我想创建一份包含不同测试的所有患者的列表以及访问日期。如何进行此检查?
是否存在类似&#34;由群组申请&#34;功能
因此,这个例子的结果将是3号患者3 (因为他参加了测试B和其他测试,其他时间点是测试A)
答案 0 :(得分:2)
一些不同的选择:
ave
:
df[as.logical(ave(as.character(df$Test),df$Patient,FUN=function(x) x != x[1] )),]
# Patient VisitDate Test
#11 3 3mth B
by
:
do.call(rbind, by(df, df$Patient, function(x) x[x$Test != x$Test[1],] ))
# Patient VisitDate Test
#3 3 3mth B
data.table
的{{1}}:
by