jonnie
jonnie

Reputation: 765

check a vector and output a singular TRUE or FALSE value

I am checking for duplicate dates in a data set with the following command:

duplicateTest <- duplicated(index(closePricesClean))

The output for this is a "FALSE" for every date not duplicated. So, if closePricesClean has 500 observations, then duplicateTest will return a list of 500 "TRUE" or "FALSE" values. What I'd like instead is to return a single "FALSE" value if the entire vector is "FALSE" or "TRUE" if the list contains even one "TRUE" value.

Do I need to construct an if statement? Or is there a function that I don't know about?

Upvotes: 1

Views: 2697

Answers (2)

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

Use anyDuplicated and check whether or not its result equals zero. This will be faster than using any on the results of duplicated because it will stop as soon as the first TRUE is encountered.

duplicateTest <- anyDuplicated(index(closePricesClean)) != 0

Upvotes: 8

Luciano Selzer
Luciano Selzer

Reputation: 10016

any is what you are looking for. From the help: Given a set of logical vectors, is at least one of the values true?

any(duplicateTest)

Upvotes: 4

Related Questions