Home > Enterprise >  How to create a incidence matrix in Julia Dataframes
How to create a incidence matrix in Julia Dataframes

Time:09-29

going on with my studies I am struggling to find a way to create incidence matrices. I found that "solution" df

Now I would like to convert that Dataframe into an incidence matrix consisting only of "0", "1" (Starting Point), and "-1" (Endpoint). It should look like this (except their should be 1 and -1 in the matrix...but i do not how to do so...)

enter image description here

Thanks for your help!

CodePudding user response:

Here is how you can do it:

julia> using DataFrames

julia> df = DataFrame(Lines = (1:20), From = rand(1:10,20), To = rand(1:10,20))
20×3 DataFrame
 Row │ Lines  From   To    
     │ Int64  Int64  Int64 
─────┼─────────────────────
   11      4      9
   22      3      8
   33      9      1
   44      3      9
   55      4      1
   66      9      8
   77      3      4
   88      8      2
   99      3      7
  1010      6      5
  1111      7      9
  1212      8      9
  1313      8      6
  1414      9      3
  1515      3      6
  1616      5     10
  1717      3      1
  1818      8      5
  1919      2      9
  2020      5      6

julia> let imat = zeros(Int, nrow(df), max(maximum(df.From), maximum(df.To))) # assuming nodes start being numbered from 1
           for (i, (from, to)) in enumerate(zip(df.From, df.To))
               imat[i, from] = 1
               imat[i, to] = -1
           end
           res = DataFrame(imat, Symbol.(axes(imat, 2)))
           insertcols!(res, 1, :Lines => df.Lines)
       end
20×11 DataFrame
 Row │ Lines  1      2      3      4      5      6      7      8      9      10    
     │ Int64  Int64  Int64  Int64  Int64  Int64  Int64  Int64  Int64  Int64  Int64 
─────┼─────────────────────────────────────────────────────────────────────────────
   11      0      0      0      1      0      0      0      0     -1      0
   22      0      0      1      0      0      0      0     -1      0      0
   33     -1      0      0      0      0      0      0      0      1      0
   44      0      0      1      0      0      0      0      0     -1      0
   55     -1      0      0      1      0      0      0      0      0      0
   66      0      0      0      0      0      0      0     -1      1      0
   77      0      0      1     -1      0      0      0      0      0      0
   88      0     -1      0      0      0      0      0      1      0      0
   99      0      0      1      0      0      0     -1      0      0      0
  1010      0      0      0      0     -1      1      0      0      0      0
  1111      0      0      0      0      0      0      1      0     -1      0
  1212      0      0      0      0      0      0      0      1     -1      0
  1313      0      0      0      0      0     -1      0      1      0      0
  1414      0      0     -1      0      0      0      0      0      1      0
  1515      0      0      1      0      0     -1      0      0      0      0
  1616      0      0      0      0      1      0      0      0      0     -1
  1717     -1      0      1      0      0      0      0      0      0      0
  1818      0      0      0      0     -1      0      0      1      0      0
  1919      0      1      0      0      0      0      0      0     -1      0
  2020      0      0      0      0      1     -1      0      0      0      0

In the solution I used let to ensure that the operation is fast.

CodePudding user response:

Another possible way is by using broadcasting:

inci=zeros(Int,20,10)
setindex!.(Ref(inci), 1, df.Lines, df.From)
setindex!.(Ref(inci), -1, df.Lines, df.To)

This yields a Matrix that of course can be converted to a DataFrame whenever needed:

julia> inci
20×10 Matrix{Int64}:
  0  -1   0   0   1   0   0   0  0   0
  0   0   0   0   0   1   0  -1  0   0
  0   0   0   0   0   0   0  -1  1   0
  0  -1   0   0   0   0   0   1  0   0
  0   0   0  -1   0   0   1   0  0   0
  0   1  -1   0   0   0   0   0  0   0
  0   0   0   0   0   0   0   0  1  -1
  0   1  -1   0   0   0   0   0  0   0
  0   0   1   0  -1   0   0   0  0   0
  0   0   0   0   0   0  -1   0  0   0
 -1   0   0   0   1   0   0   0  0   0
  1   0   0   0   0   0  -1   0  0   0
  0   0   0   0   0   0  -1   0  1   0
  1   0   0   0  -1   0   0   0  0   0
 -1   0   0   0   0   0   0   0  0   1
  0   0   0  -1   0   0   0   0  0   1
  0   0   0   0   0  -1   0   0  0   1
  0   0   0   0   0   1   0   0  0  -1
  0   0   0   0   0   0  -1   0  0   0
  0   0  -1   0   0   0   0   1  0   0
  • Related