合并相同data.frame中的行[R]

时间:2017-08-15 19:16:20

标签: r dataframe duplicates

我有一个具有特定格式的data.frame:

df <- structure(list(Left.align = c("A", "B", "C", "F"), Right.align = c("B", 
"A", "D", "E"), Center.align = c(2, 2, 3, 6)), .Names = c("Left.align", 
"Right.align", "Center.align"), row.names = c(NA, -4L), class = "data.frame")

df
  Left.align Right.align Center.align
1          A           B            2
2          B           A            2
3          C           D            3
4          F           E            6

我想删除B A 2,因为结果与A B 2相同。我将如何在R中执行此操作?

谢谢!

1 个答案:

答案 0 :(得分:1)

我们可以按行sort然后使用duplicated获取逻辑索引以对数据集行进行子集化

df1[!duplicated(t(apply(df1, 1, sort))),]