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 8804747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:41:10+00:00 2026-06-14T01:41:10+00:00

I’m trying to solve the problem of having a co-occurence matrix. I have a

  • 0

I’m trying to solve the problem of having a co-occurence matrix. I have a datafile of transactions and items, and I want to see a matrix of the number of transactions where items appear together.

I’m a newbie in R programming and I’m having some fun finding out all the shortcuts that R has, rather than creating specific loops (I used to use C years ago and only sticking to Excel macros and SPSS now). I have checked the solutions here, but haven’t found one that works (the closest is the solution given here: Co-occurrence matrix using SAC? – but it produced an error message when I used projecting_tm, I suspected that the cbind wasn’t successful in my case.

Essentially I have a table containing the following:

TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1, etc.

I want to create something like:

   A B C D E F
A  0 1 1 0 1 1
B  1 0 3 1 1 0
C  1 3 0 1 0 0
D  1 1 1 0 1 1
E  1 1 0 1 0 0
F  0 1 1 1 0 0

What I did was (and you’d probably laugh at my rookie R approach):

library(igraph)
library(tnet)

trx <- read.table("FileName.txt", header=TRUE) 
transID <- t(trx[1])
items <- t(trx[2])

id_item <- cbind(items,transID)
item_item <- projecting_tm(id_item, method="sum")
item_item <- tnet_igraph(item_item,type="weighted one-mode tnet")
item_matrix <-get.adjacency(item_item,attr="weight")
item_matrix

As mentioned above the cbind was probably unsuccessful, so the projecting_tm couldn’t give me any result.

Any alternative approach or a correction to my method?

Your help would be much appreciated!

  • 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-14T01:41:11+00:00Added an answer on June 14, 2026 at 1:41 am

    I’d use a combination of the reshape2 package and matrix algebra:

    #read in your data
    dat <- read.table(text="TrxID Items Quant
    Trx1 A 3
    Trx1 B 1
    Trx1 C 1
    Trx2 E 3
    Trx2 B 1
    Trx3 B 1
    Trx3 C 4
    Trx4 D 1
    Trx4 E 1
    Trx4 A 1
    Trx5 F 5
    Trx5 B 3
    Trx5 C 2
    Trx5 D 1", header=T)
    
    #making the boolean matrix   
    library(reshape2)
    dat2 <- melt(dat)
    w <- dcast(dat2, Items~TrxID)
    x <- as.matrix(w[,-1])
    x[is.na(x)] <- 0
    x <- apply(x, 2,  function(x) as.numeric(x > 0))  #recode as 0/1
    v <- x %*% t(x)                                   #the magic matrix 
    diag(v) <- 0                                      #repalce diagonal
    dimnames(v) <- list(w[, 1], w[,1])                #name the dimensions
    v
    

    For the graphing maybe…

    g <- graph.adjacency(v, weighted=TRUE, mode ='undirected')
    g <- simplify(g)
    # set labels and degrees of vertices
    V(g)$label <- V(g)$name
    V(g)$degree <- degree(g)
    plot(g)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and

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.