Exporting determined degree values for certain nodes

I’ve developed an edgelist and would like to return the degree values I receive when using the simple “degree” function to a format I can easily read such as .csv or .txt file.

My imagination would have it so in one column the nodes would be listed while the second column the degree values.

Is there any method that currently exists that allows me to do this?
Thank you all for your time and help :slight_smile:

I’m not too experienced with R, but this should work:

g <- sample_gnm(10, 20)

df <- data.frame(vertex=as_ids(V(g)), degree=degree(g))

Then you can export df using write.csv, e.g. write.csv(df, 'myfile.csv', row.names=F).

Another way is to first save the degrees as a vertex attribute, then use as_data_frame():

V(g)$degree <- degree(g)
df <- as_data_frame(g, what='vertices')
1 Like