从数据框中删除"重复"行(它们在几列中有所不同)

Sha*_*ver 2 r

所以我对这个问题有类似的问题: 删除R中的重复行

在我的情况下,我想保留所有列(不像它建议使用unique前3列的功能).我想从数据框中只考虑2列,如果两个提到的列中的"值"相同,则只保留1行.

数据看起来像:

structure(list(P1 = structure(c(1L, 1L, 3L, 3L, 5L, 5L, 5L, 5L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 2L), .Label = c("Apple", 
"Grape", "Orange", "Peach", "Tomato"), class = "factor"), P2 = structure(c(4L, 
4L, 3L, 3L, 5L, 5L, 5L, 5L, 6L, 6L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 6L, 6L), .Label = c("Banana", "Cucumber", "Lemon", "Orange", 
"Potato", "Tomato"), class = "factor"), P1_location_subacon = structure(c(NA, 
NA, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Fridge", "Table"), class = "factor"), 
    P1_location_all_predictors = structure(c(2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L), .Label = c("Table,Desk,Bag,Fridge,Bed,Shelf,Chair", 
    "Table,Shelf,Cupboard,Bed,Fridge", "Table,Shelf,Fridge"), class = "factor"), 
    P2_location_subacon = structure(c(1L, 1L, 1L, 1L, NA, NA, 
    NA, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Fridge", 
    "Shelf"), class = "factor"), P2_location_all_predictors = structure(c(3L, 
    3L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 3L), .Label = c("Shelf,Fridge", "Shelf,Fridge,Bed", 
    "Table,Shelf,Fridge"), class = "factor")), .Names = c("P1", 
"P2", "P1_location_subacon", "P1_location_all_predictors", "P2_location_subacon", 
"P2_location_all_predictors"), row.names = c(NA, -20L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

对我来说重要的专栏是:P1P2.我想只保留其中一行,我们可以使用相同的水果/蔬菜.(记住两个水果/蔬菜必须相同):

例:

之前:

       P1       P2 P1_location_subacon            P1_location_all_predictors P2_location_subacon P2_location_all_predictors
1   Apple   Orange                <NA>       Table,Shelf,Cupboard,Bed,Fridge              Fridge         Table,Shelf,Fridge
2   Apple   Orange                <NA>       Table,Shelf,Cupboard,Bed,Fridge              Fridge         Table,Shelf,Fridge
3  Orange    Lemon              Fridge                    Table,Shelf,Fridge              Fridge           Shelf,Fridge,Bed
4  Orange    Lemon              Fridge                    Table,Shelf,Fridge              Fridge           Shelf,Fridge,Bed
5  Tomato   Potato              Fridge                    Table,Shelf,Fridge                <NA>               Shelf,Fridge
6  Tomato   Potato              Fridge                    Table,Shelf,Fridge                <NA>               Shelf,Fridge
7  Tomato   Potato              Fridge                    Table,Shelf,Fridge                <NA>               Shelf,Fridge
8  Tomato   Potato              Fridge                    Table,Shelf,Fridge                <NA>               Shelf,Fridge
Run Code Online (Sandbox Code Playgroud)

后:

    P1       P2 P1_location_subacon            P1_location_all_predictors P2_location_subacon P2_location_all_predictors
1   Apple   Orange                <NA>       Table,Shelf,Cupboard,Bed,Fridge              Fridge         Table,Shelf,Fridge
4  Orange    Lemon              Fridge                    Table,Shelf,Fridge              Fridge           Shelf,Fridge,Bed
5  Tomato   Potato              Fridge                    Table,Shelf,Fridge                <NA>               Shelf,Fridge
Run Code Online (Sandbox Code Playgroud)

它将保留哪一行并不重要.这可以随机选择.

MrF*_*ick 6

只需使用duplicated()您希望确保唯一的列子集,并使用它来对主data.frame进行子集化.例如

dd[ !duplicated(dd[,c("P1","P2")]) , ]
Run Code Online (Sandbox Code Playgroud)