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

  • Home
  • SEARCH
  • 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 8716169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:00:55+00:00 2026-06-13T06:00:55+00:00

I am trying to compute a pairwise matrix in R that counts the number

  • 0

I am trying to compute a pairwise matrix in R that counts the number of times individuals interact with other individuals (so the matrix will include N number of rows and columns corresponding to number of individuals). I have a dataframe that lists “actors” and “partners” in separate columns.

nn <- data.frame(actors=c('DOL','DOL','DOL','DOL','DOL','NOR','NOR','NOR','NIN','JOJ'),partners=c('JOJ','JOJ','NOR','NOR','NIN','NIN','DOL','JOJ','NOR','NOR'))

The data are such that direction of the interaction is irrelevant, so each cell should count the number of times individual X acts on Y plus the number of times Y acts on X. Ideally, the data frame above should give a matrix that looks like this:

     DOL JOJ NOR NIN
DOL    0   2   3   1
JOJ    2   0   2   0
NOR    3   2   0   2
NIN    1   0   2   0

I started writing a loop to cycle through each individual in my dataset and to count his/her interactions both from actor->partner and partner->actor. I’m sure this would work, but is not ideal as the full dataset is quite large. Is there a better way?


Update:
Thanks for the responses! Both solutions work great! I’m posting my implementation of Josh’s suggestion, which was very helpful.

x <- with(nn, table(actors, partners))
y <- t(x)

# unique individuals
u <- unique(c(rownames(x),colnames(x)))

m <- matrix(0,ncol=length(u),nrow=length(u),dimnames=list(u,u))

i1 <- as.matrix(expand.grid(rownames(x),colnames(x)))
i2 <- as.matrix(expand.grid(rownames(y),colnames(y)))

m[i1] <- x[i1]
m[i2] <- m[i2] + y[i2]
  • 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-13T06:00:56+00:00Added an answer on June 13, 2026 at 6:00 am

    Base R’s table() will get you what you’re after:

    x <- with(nn, table(actors, partners))
    x + t(x)
    #       partners
    # actors DOL JOJ NIN NOR
    #    DOL   0   2   1   3
    #    JOJ   2   0   0   2
    #    NIN   1   0   0   2
    #    NOR   3   2   2   0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compute and count the number of rows inside a C#
Trying to compute a harmonic series. Right now I'm entering the number I want
I am trying to find a program in C code that will allow me
I have a sum that I'm trying to compute, and I'm having difficulty parallelizing
I am trying to compute a rolling window (shifting by 1 day) covariance matrix
I am trying to compute the cube root of a negative number in VBA
While trying to compute the width of an hidden element I found that jquery.width()
I'm trying to compute de Bruijn sequences for alphabets which have a number of
I'm trying to use a matrix to compute stuff. The code is this import
I'm trying to write C++ code which will compute all possible orderings of groups

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.