Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

R's duplicated returns a vector showing whether each element of a vector or data frame is a duplicate of an element with a smaller subscript. So if rows 3, 4, and 5 of a 5-row data frame are the same, duplicated will give me the vector

FALSE, FALSE, FALSE, TRUE, TRUE

But in this case, I actually want to get

FALSE, FALSE, TRUE, TRUE, TRUE

that is, I want to know whether a row is duplicated by a row with a larger subscript too.

1 Answer

0 votes
by

To find the duplicate rows including elements with smaller subscripts, you can use the fromLast argument of the duplicated function i.e., call the duplicated function twice, once with fromLast=FALSE and once with fromLast=TRUE and take the rows where either are TRUE.

For example:

v <- c("a", "b", "f","f","f") 

 v[duplicated(v) | duplicated(v, fromLast=TRUE)]

[1] "f" "f" "f"

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...