Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:49:19+00:00 2026-06-13T19:49:19+00:00

I have a ‘long-form’ data frame with columns id (the primary key) and featureCode

  • 0

I have a ‘long-form’ data frame with columns id (the primary key) and featureCode (categorical variable). Each record has between 1 and 9 values of the categorical variable. For example:

id  featureCode
5   PPLC
5   PCLI
6   PPLC
6   PCLI
7   PPL
7   PPLC
7   PCLI
8   PPLC
9   PPLC
10  PPLC

I’d like to calculate the number of times each feature code is used with the other feature codes (the “pairwise counts” of the title). At this stage, the order each feature code is used is not important. I envisage the result would be another data frame, where the rows and columns are feature codes, and the cells are counts. For example:

      PPLC  PCLI  PPL
PPLC  0     3     1
PCLI  3     0     1
PPL   1     1     0

Unfortunately, I don’t know how to perform this calculation and I’ve drawn a blank when searching for advice (mostly, I suspect, because I don’t know the correct terminology).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:49:22+00:00Added an answer on June 13, 2026 at 7:49 pm

    Here is a data.table approach similar to @mrdwab

    It will work best if featureCode is a character

    library(data.table)
    
    DT <- data.table(dat)
    # convert to character
    DT[, featureCode := as.character(featureCode)]
    # subset those with >1 per id
    DT2 <- DT[, N := .N, by = id][N>1]
    # create all combinations of 2
    # return as a data.table with these as columns `V1` and `V2`
    # then count the numbers in each group
    DT2[, rbindlist(combn(featureCode,2, 
          FUN = function(x) as.data.table(as.list(x)), simplify = F)), 
        by = id][, .N, by = list(V1,V2)]
    
    
         V1   V2 N
    1: PPLC PCLI 3
    2:  PPL PPLC 1
    3:  PPL PCLI 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have have this line of code in my form when I create a new
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
I have a text area in my form which accepts all possible characters from
Have the following code: String s= v.request(engine/?key=, P4z72NmBa91&method=load); JSONParser parser = new JSONParser(); Object
Have whats probably a simple problem with using parent in if and each statements
Have a text box which get data for price. If someone enter something like
Have the following code. The height is variable so the height of the floating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.