[英]Exclude subsequent duplicated rows
I would like to exclude all duplicated rows. 我想排除所有重复的行。 However, it has to be true just when they are subsequent rows.
但是,只有当它们是后续行时才必须如此。 Follows a representative example:
遵循一个代表性的例子:
My input df
: 我的输入
df
:
df <- "NAME VALUE
Prb1 0.05
Prb2 0.05
Prb3 0.05
Prb4 0.06
Prb5 0.06
Prb6 0.01
Prb7 0.10
Prb8 0.05"
df <- read.table(text=df, header=T)
My expected outdf
: 我的预期
outdf
:
outdf <- "NAME VALUE
Prb1 0.05
Prb4 0.06
Prb6 0.01
Prb7 0.10
Prb8 0.05"
outdf <- read.table(text=df, header=T)
rle()
is a nice function that identifies runs of identical values, but it can be kind of a pain to wrestle it's output into a usable form. rle()
是一个很好的函数,它可以识别相同值的运行,但是将它的输出转换为可用的形式可能会很麻烦。 Here's a relatively painless incantation that works in your case. 这是一个相对无痛的咒语,适用于你的情况。
df[sequence(rle(df$VALUE)$lengths) == 1, ]
# NAME VALUE
# 1 Prb1 0.05
# 4 Prb4 0.06
# 6 Prb6 0.01
# 7 Prb7 0.10
# 8 Prb8 0.05
There are probably many ways of solving this, I would try rleid/unique
combination from the data.table
devel version 可能有很多方法可以解决这个问题,我会尝试使用
data.table
devel版本中的 rleid/unique
组合
library(data.table) ## v >= 1.9.5
unique(setDT(df)[, indx := rleid(VALUE)], by = "indx")
# NAME VALUE indx
# 1: Prb1 0.05 1
# 2: Prb4 0.06 2
# 3: Prb6 0.01 3
# 4: Prb7 0.10 4
# 5: Prb8 0.05 5
Or from some great suggestions from comments: 或者从评论中提出一些很好的建议:
Using just the new shift
function 仅使用新的
shift
功能
setDT(df)[VALUE != shift(VALUE, fill = TRUE)]
Or using duplicated
combined with rleid
或者使用
duplicated
结合rleid
setDT(df)[!duplicated(rleid(VALUE)), ]
How about this: 这个怎么样:
> df[c(T, df[-nrow(df),-1] != df[-1,-1]), ]
NAME VALUE
1 Prb1 0.05
4 Prb4 0.06
6 Prb6 0.01
7 Prb7 0.10
8 Prb8 0.05
Here, df[-nrow(df),-1] != df[-1,-1]
finds pairs of consecutive rows that contain different values, and the rest of the code extracts them from the dataframe. 这里,
df[-nrow(df),-1] != df[-1,-1]
查找包含不同值的连续行对,其余代码从数据帧中提取它们。
I would use a solution similar to @NPE 's 我会使用类似于@NPE的解决方案
df[c(TRUE,abs(diff(df$VALUE))>1e-6),]
Of course you can use any other tolerance level (other than 1e-6
). 当然,您可以使用任何其他容差级别(
1e-6
除外)。
I came across this nice function a while ago which flags rows as being first based upon a specified variable: 我刚才遇到了这个很好的函数,它首先根据指定的变量标记行:
isFirst <- function(x,...) {
lengthX <- length(x)
if (lengthX == 0) return(logical(0))
retVal <- c(TRUE, x[-1]!=x[-lengthX])
for(arg in list(...)) {
stopifnot(lengthX == length(arg))
retVal <- retVal | c(TRUE, arg[-1]!=arg[-lengthX])
}
if (any(missing<-is.na(retVal))) # match rle: NA!=NA
retVal[missing] <- TRUE
retVal
}
Applying it to your data gives: 将其应用于您的数据会给出:
> df$first <- isFirst(df$VALUE)
> df
NAME VALUE first
1 Prb1 0.05 TRUE
2 Prb2 0.05 FALSE
3 Prb3 0.05 FALSE
4 Prb4 0.06 TRUE
5 Prb5 0.06 FALSE
6 Prb6 0.01 TRUE
7 Prb7 0.10 TRUE
8 Prb8 0.05 TRUE
You can then dedup on the first column to get your expected output. 然后,您可以在第一列上进行重复数据删除以获得预期输出。
I've found this very useful in the past, especially coming from a SAS background where this was very easy to do. 我发现这在过去非常有用,特别是来自SAS背景,这很容易做到。
已有很多好的答案,这里是dplyr
版本:
filter(df,VALUE!=lag(VALUE,default=df$VALUE[1]+1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.