R - 删除连续(仅)重复 [英] R - delete consecutive (ONLY) duplicates

查看:18
本文介绍了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 和其他一些帖子的指示,我尝试使用 duplicated 功能:

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

不知道为什么我最后得到了 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 - 删除连续(仅)重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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