Here we are explaining how to write an R program to list the distinct values from a given vector. Here we are using a built-in function unique() for this finding. This function helps to find the distinct or unique values from the vector by eliminating duplicate values. The syntax of this function is
Here x is the vector, matrix, or data frame, returns a similar object whose duplicate elements need to be eliminated.
Below are the steps used in the R program to get the distinct values from a given vector. In this R program, we directly give the values to built-in functions. For that first, we consider the variable A in which vector value is assigned. Finally, call the unique function as unique(A) for finding the distinct element.
STEP 1: Assign variable A with vector values
STEP 2: First print original vector values
STEP 3: Call the function unique as unique(A)
STEP 5: print the result of the function
A = c(25, 25, 10, 15, 20, 20, 30, 40) print("Original vector:") print(A) print("Distinct values of the vector:") print(unique(A))
 "Original vector:"  25, 25, 10, 15, 20, 20, 30, 40)  "Distinct values of the vector:"  10 15 20 25 30 40