Home > OS >  Count number of occurances of pairs of values that are in different order in dataframe in R
Count number of occurances of pairs of values that are in different order in dataframe in R

Time:08-09

I have an R dataframe with two columns

    data <- data.frame(
Process1 = c("A","B","C"),
Process2 = c("B","A","E")
    )

In the context I am working in, the pairs A-B and B-A are considered the same. How can I count the number of occurances of each value pair, without A-B and B-A pairs counted as different values?

The expected output would be

Process_pair Counts
A-B 2
C-E 1

CodePudding user response:

This is my solution based on your example. If your real problem is more complex, this solution would need some changes:

data$index <- apply(data, 1, function(x) paste(sort(x), collapse = "-"))

table(data$index)

CodePudding user response:

This is a solution using dplyr

library(dplyr)

data %>%
  mutate(Process_pair = if_else(
    Process1 > Process2, 
    true = paste(Process2, Process1, sep = "-"), 
    false = paste(Process1, Process2, sep = "-")
  )) %>%
  count(Process_pair)
  • Related