Categories
Uncategorized

Degree Centrality Table for NCols – R

This little function calculates Degree Centrality (CD) for each node in an undirected simplified graph. The node column also has to be entered as data frame.

get.cd.table<-function(dfx){

	colnames(dfx)<-c("x","x");
	dfx<-rbind(dfx[1],dfx[2]);
	dfx<-as.data.frame(table(dfx))
	dfx<-dfx[with(dfx,order(-Freq)),]
	rownames(dfx) <- 1:nrow(dfx)
	dfx$Cd<-dfx[2]/(nrow(dfx)-1)
	colnames(dfx)<-c("user","Freq","CD")
	dfx

}

Then you get it to work like this…

df<-data.frame(read.table("location/of/simplified_ncol.txt"))

get.cd.table(df)

And that should return something like

           user Freq        CD
1         user1  114 0.716981132
2         user2   30 0.188679245
3         user3   19 0.119496855
4         user4   12 0.075471698
5         user5   12 0.075471698
6         user6   12 0.075471698
7         user7   10 0.062893082
8         user8    8 0.050314465
9         user9    8 0.050314465

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s