R:如何按列分组并删除每个组的另一列中包含重复的行

时间:2014-07-08 14:25:56

标签: r unique

如果有一个数据框,我需要按“ID”对数据框进行分组,然后删除列为“course”的重复值的行?

    ID       time  course
1 ID01 01.10.2012    Math
2 ID01 10.11.2012    Math
3 ID01 01.01.2013    Math
4 ID01 01.01.2013 History
5 ID02 10.01.2013    Math
6 ID02 02.05.2013    Math
7 ID02 15.01.2013 History 

我需要这个:

 1 ID01 01.10.2012    Math
 4 ID01 01.01.2013 History
 5 ID02 10.01.2013    Math
 7 ID02 15.01.2013 History

dput(test)
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("ID01", 
"ID02"), class = "factor"), time = structure(c(1L, 2L, 3L, 3L, 
4L, 5L, 6L), .Label = c("01.10.2012", "10.11.2012", "01.01.2013", 
"10.01.2013", "02.05.2013", "15.01.2013"), class = "factor"), 
    course = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 2L), .Label = c("Math", 
    "History"), class = "factor")), .Names = c("ID", "time", 
"course"), row.names = c(NA, 7L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

ff=your.table
ff[!duplicated(ff[c("ID","course")]),]