List distinct values in a vector in R
Asked Answered
S

7

120

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_code
FROM data
Saltwater answered 13/10, 2011 at 13:57 Comment(0)
C
197

Do you mean unique:

R> x = c(1,1,2,3,4,4,4)
R> x
[1] 1 1 2 3 4 4 4
R> unique(x)
[1] 1 2 3 4
Carlson answered 13/10, 2011 at 14:8 Comment(0)
O
17

If the data is actually a factor then you can use the levels() function, e.g.

levels( data$product_code )

If it's not a factor, but it should be, you can convert it to factor first by using the factor() function, e.g.

levels( factor( data$product_code ) )

Another option, as mentioned above, is the unique() function:

unique( data$product_code )

The main difference between the two (when applied to a factor) is that levels will return a character vector in the order of levels, including any levels that are coded but do not occur. unique will return a factor in the order the values first appear, with any non-occurring levels omitted (though still included in levels of the returned factor).

Ojibwa answered 18/4, 2018 at 3:11 Comment(0)
M
8

Try using the duplicated function in combination with the negation operator "!".

Example:

wdups <- rep(1:5,5)
wodups <- wdups[which(!duplicated(wdups))]

Hope that helps.

Mercantile answered 13/10, 2011 at 14:7 Comment(0)
A
7

You can also use the sqldf package in R.

Z <- sqldf('SELECT DISTINCT tablename.columnname FROM tablename ')
Amadus answered 31/3, 2016 at 23:43 Comment(0)
T
0

another way would be to use dplyr package:

x = c(1,1,2,3,4,4,4)
dplyr::distinct(as.data.frame(x))
Topliffe answered 25/9, 2020 at 7:18 Comment(0)
S
0

In R Language (version 3.0+) You can apply filter to get unique out of a list-

data.list <- data.list %>% unique

or couple it with other operation as well

data.list.rollnumbers <- data.list %>% pull(RollNumber) %>% unique

unique doesn't require dplyr.

Salep answered 5/1, 2021 at 7:38 Comment(2)
is there a function that replaces "pull" and "unique" altogether?Choline
Currently I have not got such short notation in R. Python might be having it.Salep
L
0

this may work as well,

1) unlist(lapply(mtcars, function(x) length(unique(x))))
2) lapply(mtcars, function(x) unique(x))

outcomes,

  1. mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
     25    3   27   22   22   29   30    2    2    3    6 
    
  2. $mpg
    [1] 21.0 22.8 21.4 18.7 18.1 14.3 24.4 19.2 17.8 16.4 17.3 15.2 10.4 14.7 32.4 30.4 33.9 21.5 15.5 13.3 27.3 26.0 15.8 19.7 15.0
    $cyl
    [1] 6 4 8
    $ and so on....
    
Levinson answered 27/4, 2021 at 8:35 Comment(1)
This won't give output as expected by OP!!Exodontics

© 2022 - 2024 — McMap. All rights reserved.