本文介绍了R-删除连续(仅)重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于给定列中值的重复来消除数据帧中的行,但仅限于那些连续的值。
例如,对于以下数据框:

I need to eliminate rows from a data frame based on the repetition of values in a given column, but only those that are consecutive.For example, for the following data frame:

df = data.frame(x=c(1,1,1,2,2,4,2,2,1))
df$y <- c(10,11,30,12,49,13,12,49,30)
df$z <- c(1,2,3,4,5,6,7,8,9)

x  y z
1 10 1
1 11 2
1 30 3
2 12 4
2 49 5
4 13 6
2 12 7
2 49 8
1 30 9

我需要消除x列中具有连续重复值的行,保留最后重复的行,并保持数据框的结构:

I would need to eliminate rows with consecutive repeated values in the x column, keep the last repeated row, and maintain the structure of the data frame:

x  y z
1 30 3
2 49 5
4 13 6
2 49 8
1 30 9

help 和其他一些帖子,我尝试使用重复的函数:

Following directions from help and some other posts, I have tried using the duplicated function:

df[ !duplicated(x,fromLast=TRUE), ] # which gives me this:
      x  y  z
1     1 10  1
6     4 13  6
7     2 12  7
9     1 30  9
NA   NA NA NA
NA.1 NA NA NA
NA.2 NA NA NA
NA.3 NA NA NA
NA.4 NA NA NA
NA.5 NA NA NA
NA.6 NA NA NA
NA.7 NA NA NA
NA.8 NA NA NA

不确定我为什么要在结束(这不是我测试过的类似表),但是只对部分值起作用。

Not sure why I get the NA rows at the end (wasn't happening with a similar table I was testing), but works only partially on the values.

我也尝试过使用 data.table 软件包如下:

I have also tried using the data.table package as follows:

library(data.table)
dt <- as.data.table(df)
setkey(dt, x)
dt[J(unique(x)), mult ='last']

效果很好,但是它消除了数据框中的所有重复项,而不仅仅是连续的,因此提供了这样的信息:

Works great, but it eliminates ALL duplicates from the data frame, not just those that are consecutive, giving something like this:

x  y z
1 30 9
2 49 8
4 13 6

请,如果交叉发布,请原谅。我尝试了一些建议,但没有一个能够消除仅那些连续的建议。
我将不胜感激。

Please, forgive if cross-posting. I tried some of the suggestions but none worked for eliminating only those that are consecutive.I would appreciate any help.

谢谢

推荐答案

您只需要检查数字后是否没有重复项,即x [i + 1]!= x [i]并注意最后一个值将始终存在。

You just need to check in there is no duplicate following a number, i.e x[i+1] != x[i] and note the last value will always be present.

df[c(df$x[-1] != df$x[-nrow(df)],TRUE),]
  x  y z
3 1 30 3
5 2 49 5
6 4 13 6
8 2 49 8
9 1 30 9

这篇关于R-删除连续(仅)重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:58