如果value中的a值等于R中的前一行,则删除整行 [英] Delete the entire row if the a value in value is equal to previous row in R

查看:100
本文介绍了如果value中的a值等于R中的前一行,则删除整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R编程的新手,我需要帮助才能根据单个列的值删除整行.如果单列中的值等于上一行的值,我想删除该行.

I am new to R programming and I need a help to delete the entire row based on the value of a single column. I want to delete the row, if a value in a single column is equal to the previous row value.

以下是我的数据,

   x.id x.timestamp x.count
71    1  1435114605      61
72    1  1435114606      61
73    1  1435114659      61
74    1  1435114719      62
75    1  1435114726      62
76    1  1435114780      62
77    1  1435155998      62
78    1  1435156059      62
79    1  1435156076      62
80    1  1435156119      62

在这里,我要基于x $ x.count值删除行.

Here I want to delete the rows based on the x$x.count value.

我的输出应该是

   x.id x.timestamp x.count
71    1  1435114605      61
74    1  1435114719      62

我在这里不能使用重复或唯一函数,因为稍后在数据集中重复相同的值.我只想根据先前的值删除数据.

I cant use duplicated or unique function here because later on the same values repeat in the data set. I just want to remove the data based on the previous value.

推荐答案

您可以使用diff查找连续行之间的差大于0(加上第一行)的位置.如果x.count未排序,则可以使用!=0.

You could use diff to find where differences between consecutive rows are greater than 0 (plus the first row). Maybe use !=0 if the x.count isn't sorted.

dat[c(T, diff(dat$x.count)>0), ]

#    x.id x.timestamp x.count
# 71    1  1435114605      61
# 74    1  1435114719      62

这篇关于如果value中的a值等于R中的前一行,则删除整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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