How to remove consecutive duplicate entries in R? I think with
may be used but can't think how to use it. Illustrating one example:
read.table(text = "
a t1
b t2
b t3
b t4
c t5
c t6
b t7
d t8")
Sample Data: D
events time
a t1
b t2
b t3
b t4
c t5
c t6
b t7
d t8
Required Outcome:
events time
a t1
b t4
c t6
b t7
d t8
`
If you use 'uniq' command without any arguments, it will remove all consecutive duplicate lines and display only the unique lines.
Yet an other one, assuming your data.frmae
is named d
:
d[cumsum(rle(as.numeric(d[,1]))$lengths),]
V1 V2
1 a t1
4 b t4
6 c t6
7 b t7
8 d t8
EDIT: Not exactly correct as it only shows one b row. You can also use the duplicated() function
x <- read.table(text = " events time
a t1
b t2
b t3
b t4
c t5
c t6
d t7", header = TRUE)
#Making sure the data is correctly ordered!
x <- x[order(x[,1], x[,2]), ]
x[!duplicated(x[,1], fromLast=TRUE), ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With