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