This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
# 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################## | |
# 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------------------------------------------------------------- | |
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--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{, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------------------------------------------------- | |
# [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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(small) | |
+geom_point(aes(x=carat,y=price,colour=cut)) | |
+scale_y_log10() | |
+facet_wrap(~cut) | |
+opts(title="First example") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |