List distinct values in a vector in R

256,440

Solution 1

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

Solution 2

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).

Solution 3

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.

Solution 4

You can also use the sqldf package in R.

Z <- sqldf('SELECT DISTINCT tablename.columnname FROM tablename ')
Share:
256,440

Related videos on Youtube

Mehper C. Palavuzlar
Author by

Mehper C. Palavuzlar

about.me/mehper Industrial Engineer M.Sc. One of the authors of Distribution Planning of Magazines: A Practical Approach. Author of Random Variate Generation If the Density Is Not Known: Basics, Methods, Implementations. Mostly dealing with the following topics: Food Logistics, Enterprise Resources Planning, Supply Chain Management, Materials Management, Healthcare Logistics, Executive Reporting, Data Analysis, System Development and Optimization. Programming Languages: VBA, SQL, R. XBox 360 fan.

Updated on April 27, 2021

Comments

  • Mehper C. Palavuzlar
    Mehper C. Palavuzlar about 3 years

    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
    
  • AnilGoyal
    AnilGoyal about 3 years
    This won't give output as expected by OP!!
  • Alvaro Morales
    Alvaro Morales almost 3 years
    is there a function that replaces "pull" and "unique" altogether?
  • Vishal Kumar Sahu
    Vishal Kumar Sahu almost 3 years
    Currently I have not got such short notation in R. Python might be having it.