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

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

问题描述

我有以下数据:

  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行?输出的格式如下:

  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 53
34 14 45 20
2 8 18 17
2 8 18 5
/ pre>

在最后一列中找到值的总和:

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


解决方案

您可以使用重复的来执行此操作,该检查在传递矩阵时检查重复的行。因为你只是检查前三列,你应该通过 dat [, - 4] 到函数。

  dat [duplicateated(dat [, -  4])|重复(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


I have the following data:

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

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.

ADDITIONAL QUESTION: in the output

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

find the sum of values in last column:

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

解决方案

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天全站免登陆