I have a vector like this:
x = c(1,2,3,4,5,6,4,5,6,7)
> x
[1] 1 2 3 4 5 6 4 5 6 7
I want to get rid of duplicates and get something like this:
> [1] 1 2 3 7
My attempt
y = x[duplicated(x)]
> y
[1] 4 5 6
> x[x!=y]
[1] 1 2 3 7
Warning message:
In x != y : longer object length is not a multiple of shorter object length
>
What am I doing wrong?
Is this error something I should worry about?
Is there another way to do this without getting an error?
We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.
We can find the rows with duplicated values in a particular column of an R data frame by using duplicated function inside the subset function. This will return only the duplicate rows based on the column we choose that means the first unique value will not be in the output.
Beware using consecutive numbers in your tests!
x <- c(1,2,3,4,5,6,4,5,6,7)
x1 <- c(-1, -1, 2, 8, 8, 15)
keep_singles <- function(v){
v[!(v %in% v[duplicated(v)])]
}
keep_singles(x)
[1] 1 2 3 7
keep_singles(x1)
[1] 2 15
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