Convert a dataframe to a 2d array

I am new to Julia. I have an adjacency matrix of a simple graph saved in graph.csv, i.e., (i,j)=1 iff v_i is linked to v_j. I want to save only links of the graph, i.e., convert the format to a more efficient and space-saving one since my graph is sparse. My problem is how to convert an y-element Array{Any,1} of links to yx2 Array{Int64,2}.

My initial try is as follows:

graph.csv

0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0
1,0,1,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0
1,1,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0
1,1,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,1,1,0,0,0,0,0,0,0,0,1
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,1
0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,1,1
0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,0,0,0,0,1,1,1,0,1
0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,1,1,1,0,1,1,0,0,1,1,1,1,1,1,1,0

###########
code.jl

Pkg.add("DataFrames.jl");
using DataFrames;
data=readtable("graph.csv",header=false);
data2=[];
for i in 1:size(data,1)
  for j in 1+i:size(data,2)
      if data[i,j]==1
         push!(data2,[i,j])
      end
   end
end

# my problem lies here
writetable("graph2.txt",data2)
reshape(hcat(data2...),size(data2,1),:)   # works but is not convinient

Instead of reading a DataFrame and converting, I would propose DelimitedFiles.readdlm.

3 Likes

Using DelimitedFiles you could do something like this:

writedlm("graph3.csv", findall(readdlm("graph.csv", ',', Int) .== 1) .|> x -> [x[1],x[2]], ',')
3 Likes

Thanks you. It is correct. However, I would like to know how to convert a 1D array of tuples (a,b) to a 2D array i.e., an array of nx2.

Here’s one way:

julia> tuples = [(1,2), (3,4), (5,6)]
3-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (3, 4)
 (5, 6)

julia> vcat((tuples .|> t -> [t[1] t[2]])...)
3×2 Array{Int64,2}:
 1  2
 3  4
 5  6
1 Like

There’s also nothing wrong with a for loop for readability

function tuplestomat(x)
    m = length(x[1])
    n = length(x)
    M = Array{typeof(x[1][1])}(undef, m, n)
    for i in 1:n
        for j in 1:m
            M[i, j] = x[i][j]
        end
    end           
return M
end