pairwise: Count pairs of items within a group

pairwise_count_dtR Documentation

Count pairs of items within a group

Description

Count the number of times each pair of items appear together within a group. For example, this could count the number of times two words appear within documents. This function has referred to pairwise_count in widyr package, but with very different defaults on several parameters.

Usage

pairwise_count_dt(
  .data,
  .group,
  .value,
  upper = FALSE,
  diag = FALSE,
  sort = TRUE
)

Arguments

.data

A data.frame.

.group

Column name of counting group.

.value

Item to count pairs, will end up in V1 and V2 columns.

upper

When FALSE(Default), duplicated combinations would be removed.

diag

Whether to include diagonal (V1==V2) in output. Default uses FALSE.

sort

Whether to sort rows by counts. Default uses TRUE.

Value

A data.table with 3 columns (named as "V1","V2" and "n"), containing combinations in "V1" and "V2", and counts in "n".

See Also

pairwise_count

Examples


dat <- data.table(group = rep(1:5, each = 2),
              letter = c("a", "b",
                         "a", "c",
                         "a", "c",
                         "b", "e",
                         "b", "f"))
pairwise_count_dt(dat,group,letter)
pairwise_count_dt(dat,group,letter,sort = FALSE)
pairwise_count_dt(dat,group,letter,upper = TRUE)
pairwise_count_dt(dat,group,letter,diag = TRUE)
pairwise_count_dt(dat,group,letter,diag = TRUE,upper = TRUE)

# The column name could be specified using character.
pairwise_count_dt(dat,"group","letter")

tidyfst documentation built on Sept. 16, 2024, 9:06 a.m.