Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List distinct values in a vector in R

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 
like image 339
Mehper C. Palavuzlar Avatar asked Oct 13 '11 13:10

Mehper C. Palavuzlar


People also ask

How do you find the distinct values of a vector in R?

To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.

How do you find the distinct elements of a vector?

Method 1: Using unique() For this, the vector from which distinct elements are to be extracted is passed to the unique() function. The result will give all distinct values in a vector.

How do I return unique values in R?

The unique function returned all the unique values present in the dataframe by eliminating the duplicate values. Just like this, by using the unique() function in R, you can easily get the unique values present in the data.

How do I list values in a variable in R?

You can use ls() to list all variables that are created in the environment. Use ls() to display all variables. pat = " " is used for pattern matching such as ^, $, ., etc. Hope it helps!


2 Answers

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 
like image 63
csgillespie Avatar answered Oct 12 '22 18:10

csgillespie


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

like image 21
isapir Avatar answered Oct 12 '22 19:10

isapir