Back
How can I list the distinct values in a vector where the values are replicative? I mean, similarly to the following SQL statement:
SELECT DISTINCT product_codeFROM data
SELECT DISTINCT product_code
FROM data
You can use the unique function from the base package that returns a vector, data frame or array-like x but with duplicate elements/rows removed.
For example:
x = c(1,1,2,2,3,3,4,4,4)> x[1] 1 1 2 2 3 3 4 4 4> unique(x)[1] 1 2 3 4
x = c(1,1,2,2,3,3,4,4,4)
> x
[1] 1 1 2 2 3 3 4 4 4
> unique(x)
[1] 1 2 3 4
31k questions
32.8k answers
501 comments
693 users