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 / vectorize.R
Created March 25, 2013 16:37
vectorize
# STACK: Turn table (C) into (D):
# C
# a b c d
# A 3 2 . .
# B . . 1 1
# D
# A a 3
# A b 2
# A c .
@pedroj
pedroj / pairwise.R
Created March 25, 2013 16:35
pairwise
#########################################################
# Getting the pairwise interactions list
# Format: "Pant.species-Animal.species"
# assocs is a matrix (AxP). With rownames and colnames.
#
# pairwise(t(assocs)) will give the interactions list
# as: "Animal.species-Pant.species"
# Pedro Jordano. Sevilla - 15 Abr 2006 02:13:45.
#--------------------------------------------------------
pairwise<-function(assocs) {
@pedroj
pedroj / panelcorr.R
Created March 25, 2013 16:34
panelcorr
##############################################################################
# Panel with correlations for paired variables.
# Pedro Jordano. Zahara 9 Aug 2009.
##############################################################################
data(data)
panel.cor <- function(x, y, digits=2, prefix="", cex.cor)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y))
@pedroj
pedroj / Asset.R
Created March 25, 2013 16:33
Testing the difference between two CV's
#-----------------------------------------------------------------------------
# Testing the difference between two CV's
# From Example 8.12 in Zar, p. 144-145
#-----------------------------------------------------------------------------
w<-c(72.5,71.5,60.8,63.2,71.4,73.1,77.9,75.7,72.0,69.0)
h<-c(183.0,172.3,180.1,190.2,191.4,169.6,166.4,177.6,184.7,187.5,179.8)
#-----------------------------------------------------------------------------
# Testing the difference between two CV's
library(MBESS)
n1<-length(w); n2<-length(h)
@pedroj
pedroj / Asset.tex
Created March 25, 2013 16:33
#--Citing R:
#--Citing R:
citation()
To cite R in publications, use
R Development Core Team (2004). R: A language and environment for
statistical computing. R Foundation for Statistical Computing,
Vienna, Austria. ISBN 3-900051-00-3, URL http://www.R-project.org.
We have invested a lot of effort in creating R, please cite it when
using it for data analysis.
A BibTeX entry for LaTeX users is
@Manual{,
@pedroj
pedroj / Asset.R
Created March 25, 2013 16:31
Density plot
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some randomly-distributed data
nObs <- 5000
myData <- data.frame(X = rnorm(nObs), Y = rnorm(nObs))
nClusters <- 7 # Cluster it
kMeans <- kmeans(myData, centers = nClusters)
@pedroj
pedroj / Asset.R
Created March 25, 2013 16:31
Scatter plot smooth
library("geneplotter") ## from BioConductor
require("RColorBrewer") ## from CRAN
x1 <- matrix(rnorm(1e4), ncol=2)
x2 <- matrix(rnorm(1e4, mean=3, sd=1.5), ncol=2)
x <- rbind(x1,x2)
layout(matrix(1:4, ncol=2, byrow=TRUE))
op <- par(mar=rep(2,4))
smoothScatter(x, nrpoints=0)
@pedroj
pedroj / Asset.R
Created March 25, 2013 16:31
Code header
# ----------------------------------------------------------------------------
# [Title]:
# [Date]: [Loc]:
# Pedro Jordano.
# ----------------------------------------------------------------------------
## First version __DATE__. Revised __DATE__
# ----------------------------------------------------------------------------
# __DESCRIPTION__:
# mat<-read.table("mymatrix.txt") # Read a tab-separated file with 0-1,
# and no headers, just the matrix
@pedroj
pedroj / Asset.R
Created March 25, 2013 16:31
ggplot-faceted scatter plot
ggplot(small)
+geom_point(aes(x=carat,y=price,colour=cut))
+scale_y_log10()
+facet_wrap(~cut)
+opts(title="First example")
@pedroj
pedroj / Asset.R
Created March 25, 2013 16:31
Changing the order of levels of a factor
Changing the order of levels of a factor
Problem
You want to change the order in which the levels of a factor appear.
Solution
Factors in R come in two varieties: ordered and unordered, e.g., {small, medium, large} and {pen, brush, pencil}. For many analyses, it will not matter whether a factor is ordered or unordered. If the factor is ordered, then the specific order of the levels matters (small < medium < large). If the factor is unordered, then the levels will still appear in some order, but the specific order of the levels matters only for convenience (pen, pencil, brush) -- it will determine, for example, how output will be printed, or the arrangement of items on a graph.