How to extract duplicate items from a column in R language?
You can utilize the unique() function in R language to extract duplicate items from a column.
The unique() function can return a vector that contains all the unique values in the given vector.
Here is a sample code:
# 创建一个向量
vec <- c(1, 2, 3, 2, 4, 4, 5)
# 使用unique()函数提取出重复项
duplicate_items <- vec[duplicated(vec)]
# 输出结果
print(duplicate_items)
The output is:
[1] 2 4
In the above example, we start by creating a vector ‘vec’ containing duplicated elements. Then we use the function ‘duplicated()’ to check for any duplicates in the vector, which returns a logical vector indicating whether each element is a duplicate. Finally, we extract the duplicate elements by using the result of ‘duplicated()’ as an index, and store them in a vector called ‘duplicate_items’.
Note that if you only want to identify the unique values of repeated elements in a vector, without caring about their frequency or position, you can use the duplicated() function to determine if there are any duplicates, and then use the unique() function to extract those duplicates.