The “duplicated” Function in R

  • Package: Base R (No specific package, it’s a built-in function)

  • Purpose: To identify duplicated elements in a vector, data frame, or other structures.

  • General Class: Data Manipulation

  • Required Argument(s):

    • x: The input vector, data frame, or other structure to check for duplicated elements.

  • Notable Optional Arguments:

    • fromLast: A logical value indicating whether to consider duplicates from the last occurrence. The default is FALSE.

  • Example:

  • # Example usage with a vector
    my_vector <- c(1, 2, 3, 1, 2, 4, 5)
    duplicates <- duplicated(my_vector)

    print(duplicates)

    # Example usage with a data frame
    my_data_frame <- data.frame(
    Name = c("John", "Jane", "Bob", "John", "Alice"),
    Age = c(25, 30, 22, 25, 28)
    )

    duplicated_rows <- duplicated(my_data_frame)

    print(duplicated_rows)

  • In this example, duplicated is a built-in function in base R, and it can be used to identify duplicated elements in vectors or rows in a data frame. The fromLast argument is optional and allows you to control whether duplicates from the last occurrence should be considered. The result is a logical vector indicating which elements or rows are duplicated.

Previous
Previous

The “anyDuplicated” Function in R

Next
Next

The “shapiro.test” Function in R