Find Unique Values in List in R (Example)
In this tutorial you’ll learn how to get all unique elements of a list in the R programming language.
The article consists of one example for the identification of unique values. To be more precise, the post consists of the following:
Let’s dive into it…
Creation of Exemplifying Data
We’ll use the data below as basement for this R programming tutorial:
my_list <- list(l1 = letters[1:4], # Create example list l2 = letters[2:5], l3 = letters[10:7], l4 = letters[8:11]) my_list # Create example list # $l1 # [1] "a" "b" "c" "d" # # $l2 # [1] "b" "c" "d" "e" # # $l3 # [1] "j" "i" "h" "g" # # $l4 # [1] "h" "i" "j" "k" #
The previous output of the RStudio console shows the structure of our example data – It’s a list containing four list elements. Each of these list elements consists of several alphabetical letters.
Example: Identify Unique Values in List Using unlist() & unique() Functions
This example illustrates how to find all unique items of our list using the unique and unlist functions in R.
Have a look at the following R code:
unique(unlist(my_list)) # Return unique values # [1] "a" "b" "c" "d" "e" "j" "i" "h" "g" "k"
As you can see based on the previous output of the RStudio console, we have created a character vector containing all unique values of our list.
Video & Further Resources
If you need further information on the R syntax of this tutorial, you might want to have a look at the following video of my YouTube channel. In the video, I’m showing the R programming code of this article in a live session:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might read some of the related tutorials of my website. You can find a selection of related tutorials below.
- Count Unique Values in R
- Find Values Contained in First Vector but not Another
- Find Unique Combinations of All Elements from Two Vectors
- The R Programming Language
In summary: In this article, I have explained how to return all exclusive list values in the R programming language. Please let me know in the comments below, if you have any additional questions. Furthermore, don’t forget to subscribe to my email newsletter in order to receive regular updates on new articles.
Statistics Globe Newsletter