rfind-occurrences

Counting the variables that occur most often next to each other


I'm working with a large database and I want to see which variables are most often found in each other's environment (i.e. in the same row). There can be more than 20 variables in a row and I don't know all of them.

Here is an example:

  var1 <- c("x", "x", "x", "x", "x", "x", "y", "y", "y", "y")
  var2 <- c("a", "a", "b", "b", "c", "d", "e", "e", "g", "h")
  data <- data.frame(cbind(var1, var2))

The result should look like this:

|frequent contacts|n|
x&a 2
x&b 2
y&e 2
x&c 1

etc.


Solution

  • Are you looking for this?

    with(data, table(paste(var1, var2, sep = "&")))  |>
      as.data.frame()
    
    #   Var1 Freq
    # 1  x&a    2
    # 2  x&b    2
    # 3  x&c    1
    # 4  x&d    1
    # 5  y&e    2
    # 6  y&g    1
    # 7  y&h    1