rvectorfilterduplicates

Filter function in Base R


I was curious about methods to return duplicate values in a vector, list, or array in R. Focusing on a vector, I defined the following:

myvec <- c('a', letters)

which duplicates the letter 'a' in position 1 or 2 depending how you define it (position 2 in R). If I enter duplicated(myvec). I get a logical vector of F, T, F, F, ... as expected. However, when I enter:

Filter(duplicated, myvec)

The output is

character(0)

But I expect a. Why does Filter not return this value?


Solution

  • Filter calls the function duplicated for each element of vec. So, it calls duplicated(a), again duplicated(a), then duplicated(b) etc.

    As duplicated is called for each element separately, it never sees both "a"s.