Skip to content

Instantly share code, notes, and snippets.

View pedroj's full-sized avatar
🌎
Fazendo ciência e soltando pipa

Pedro Jordano pedroj

🌎
Fazendo ciência e soltando pipa
View GitHub Profile
@pedroj
pedroj / rcnames.R
Created February 20, 2012 22:54
Example of setting row and column names
# Example of setting row and column names
mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE,
dimnames = list(c("row1", "row2"),
c("C.1", "C.2", "C.3")))
mdat
@pedroj
pedroj / stack.R
Created February 20, 2012 22:56
Order columns (stack) in a table
# Order columns in a table
fakedata <- data.frame(A=c(0,0,0), X2=c(2,2,2), X1=c(1,1,1), X3=c(3,3,3))
fakedata
A X2 X1 X3
1 0 2 1 3
2 0 2 1 3
3 0 2 1 3
pos <- colnames(fakedata)[2:ncol(fakedata)]
@pedroj
pedroj / comp_graph.R
Created February 20, 2012 22:59
Comparing two networks
# For larger graphs calculate the intersection
# of the two graphs, this is reasonably fast, and then
# the Hamming distance is the number of edges in the
# symmetric difference. (If i'm right...)
int <- graph.intersection(g1,g2)
ecount(g1)+ecount(g2)-2*ecount(int)
@pedroj
pedroj / conting.R
Created February 20, 2012 23:04
Example contingency table
#--------------------------------------------------
# Contingency table
#--------------------------------------------------
# Productivity Smoking No smoking
# High 13 44
# Low 25 29
count<-c(13,25,44,29)
smoking<-factor(c("Yes","Yes","No","No"))
productivity<-factor(c("High","High","Low","Low"))
@pedroj
pedroj / readnet.R
Created February 20, 2012 23:17
Read network data
##############################################################################
### Read network data
# network
mont1<-read.paj("Monteverde.net",verbose=T)
nch1<-read.paj("nch.net",verbose=T)
hr1<-read.paj("hr.net",verbose=T)
# Initializing bipartite webs for library network
nch1<-network.initialize(dim(nch_lab)[1]+dim(nch_lab)[2],
bipartite=c(dim(nch_lab)[1]),directed=F)
nch1 <-network.bipartite(as.matrix(nch_lab),nch1)
@pedroj
pedroj / netplot.R
Created February 20, 2012 23:32
Code for plotting the networks. Library bipartite.
##############################################################################
# Code for plotting the networks. Library bipartite.
# Pedro Jordano. 2 Mar 2007. Rev 4 Apr 2008
#
#-----------------------------------------------------------------------------
# Initializing the datasets
###
# Enter the adjacency matrix directly
myweb1<- read.delim("myweb.txt", row.names=1,,header=F,sep="\t")
plotweb(nch1, method="cca", text.rot="90", labsize=0.8, col.low="green",
@pedroj
pedroj / netplot.R
Created February 20, 2012 23:34
Code for plotting the networks. Library bipartite.
##############################################################################
# Code for plotting the networks. Library bipartite.
# Pedro Jordano. 2 Mar 2007. Rev 4 Apr 2008
#-----------------------------------------------------------------------------
# Initializing the datasets
# Enter the adjacency matrix directly
myweb1<- read.delim("myweb.txt", row.names=1,,header=F,sep="\t")
plotweb(nch1, method="cca", text.rot="90", labsize=0.8, col.low="green",
col.high="yellow", col.interaction="blue")
@pedroj
pedroj / webord.R
Created February 20, 2012 23:37
Ordering matrices. Web must be a matrix with 'dimnames'
#--------------------------------------------------------------
# Ordering matrices. Web must be a matrix with 'dimnames'
#--------------------------------------------------------------
mat.ord <- function(web) {
web.ord <- web[order(rowSums(web), decreasing = TRUE),
order(colSums(web), decreasing = TRUE)]
web.ord
}
@pedroj
pedroj / plot_degdist.R
Created February 20, 2012 23:41
Plotting the degree distribution
##############################################################################
# Plotting the degree distribution.
#-----------------------------------------------------------------------------
# Assign the matrix to be analized
#-----------------------------------------------------------------------------
mat<-as.matrix(nch_lab)
#-----------------------------------------------------------------------------
# Degree
#-----------------------------------------------------------------------------
# Animals
@pedroj
pedroj / readnet.R
Created February 20, 2012 23:47
Bulk read network data.
#################################################################
# Network analyses.
# Pedro. 7 Dec 2007
#################################################################
# NOTE: These matrices have rows are animals, columns are plants.
# NOTE: JORDI's datasets for NESTEDNESS have Rows are plants,
# columns are animals.
library(bipartite)
library(networksis)
library(igraph)