如何输出重复的行 [英] How to output duplicated rows

查看:23
本文介绍了如何输出重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

x1  x2  x3  x4
34  14  45  53 
2   8   18  17
34  14  45  20
19  78  21  48 
2   8   18  5

在第 1 行和第 3 行;和 2 和 5 列 X1;X2,X3 的值相等.如何仅输出相同数量的 4 行?输出应采用以下格式:

In rows 1 and 3; and 2 and 5 the values for columns X1;X2,X3 are equal. How can I output only those 4 rows, with equal numbers? The output should be in the following format:

x1  x2  x3  x4
34  14  45  53
34  14  45  20
2   8   18  17
2   8   18  5

如果有不清楚的地方,请向我提问.

Please, ask me questions if something unclear.

附加问题:在输出中

x1  x2  x3  x4
34  14  45  53
34  14  45  20
2   8   18  17
2   8   18  5

求最后一列值的总和:

x1  x2  x3  x4
34  14  45  73
2   8   18  22

推荐答案

您可以使用 duplicated 来做到这一点,它会检​​查在传递矩阵时是否有重复的行.由于您只检查前三列,因此您应该将 dat[,-4] 传递给函数.

You can do this with duplicated, which checks for rows being duplicated when passed a matrix. Since you're only checking the first three columns, you should pass dat[,-4] to the function.

dat[duplicated(dat[,-4]) | duplicated(dat[,-4], fromLast=T),]
#   x1 x2 x3 x4
# 1 34 14 45 53
# 2  2  8 18 17
# 3 34 14 45 20
# 5  2  8 18  5

这篇关于如何输出重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆