How to Apply the duplicated Function in R (Example Code)

In this tutorial you’ll learn how to use the duplicated command in the R programming language.

Example: Applying duplicated() Function to Elements of Vector

x <- c(1:3, 2:4)                      # Constructing example vector
x                                     # Displaying vector in RStudio console
# [1] 1 2 3 2 3 4
duplicated(x)                         # Return logical vector
# [1] FALSE FALSE FALSE  TRUE  TRUE FALSE
x[!duplicated(x)]                     # Subset unique vector elements
# [1] 1 2 3 4

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top