我有以下数据:
Years A B C D
2015 1 7 1 13
2016 2 8 2 14
2017 3 9 3 15
2018 4 10 4 16
2019 5 11 5 17
2020 6 12 6 18
我希望结果显示如下(删除重复值的列):
Years A B D
2015 1 7 13
2016 2 8 14
2017 3 9 15
2018 4 10 16
2019 5 11 17
2020 6 12 18
提前感谢所有帮助!
答案 0 :(得分:3)
将函数unclass
和duplicated
组合在一起以找到匹配的列,然后取其他列:
df[!duplicated(unclass(df))]
输出:
Years A B D
<dbl> <dbl> <dbl> <dbl>
1 2015 1 7 13
2 2016 2 8 14
3 2017 3 9 15
4 2018 4 10 16
5 2019 5 11 17
6 2020 6 12 18
答案 1 :(得分:0)
如果您需要快速的操作,请尝试这种方法
df[!duplicated(as.list(df))]
# Years A B D
# 1 2015 1 7 13
# 2 2016 2 8 14
# 3 2017 3 9 15
# 4 2018 4 10 16
# 5 2019 5 11 17
# 6 2020 6 12 18
答案 2 :(得分:0)
或者我们可以转置数据集并应用duplicated
df1[!duplicated(t(df1))]
# Years A B D
#1 2015 1 7 13
#2 2016 2 8 14
#3 2017 3 9 15
#4 2018 4 10 16
#5 2019 5 11 17
#6 2020 6 12 18
df1 <- structure(list(Years = 2015:2020, A = 1:6, B = 7:12, C = 1:6,
D = 13:18), class = "data.frame", row.names = c(NA, -6L))