Skip to contents

Get paired entities from a data frame based on the other column

Usage

paired(df = ".", id, compare, idents = 2, uniq = TRUE)

Arguments

df

The data frame.

id

The column name in df for the ids to be returned in the final output

compare

The column name in df to compare the values for each id in id.

idents

The values in compare to compare. It could be either an an integer or a vector. If it is an integer, the number of values in compare must be the same as the integer for the id to be regarded as paired. If it is a vector, the values in compare must be the same as the values in idents for the id to be regarded as paired.

uniq

Whether to return unique ids or not. Default is TRUE. If FALSE, you can mutate the meta data frame with the returned ids. Non-paired ids will be NA.

Value

A vector of paired ids (in id column)

Examples

df <- data.frame(
    id = c("A", "A", "B", "B", "C", "C", "D", "D"),
    compare = c(1, 2, 1, 1, 1, 2, 1, 2)
)
paired(df, id, compare, 2)
#> [1] "A" "B" "C" "D"
paired(df, id, compare, c(1, 2))
#> [1] "A" "C" "D"
paired(df, id, compare, c(1, 2), uniq = FALSE)
#> [1] "A" "A" NA  NA  "C" "C" "D" "D"