Delete the entire row if the a value in value is equal to previous row in R

蓝咒 提交于 2019-12-24 11:28:36

问题


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.

The following is my data,

   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

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

My Output should be,

   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.


回答1:


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


来源:https://stackoverflow.com/questions/31689413/delete-the-entire-row-if-the-a-value-in-value-is-equal-to-previous-row-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!