\documentclass{article} \usepackage{graphicx} \usepackage{url} \usepackage{amssymb} \usepackage{amsmath} \newcommand{\R}{{\sf R}} \newcommand{\code}[1]{\texttt{#1}} \title{Miscellaneous \R\ notes} \author{\copyright 2005 Ben Bolker} \newcounter{exercise} \numberwithin{exercise}{section} \newcommand{\exnumber}{\addtocounter{exercise}{1} \theexercise \thinspace} \begin{document} \maketitle \begin{enumerate} \item{\textbf{installing packages without administrative privileges:} The following commands install \R\ packages (in this case the \code{plotrix}, \code{gplots}, \code{gtools} and \code{gdata} packages) into a working folder and then attach them. First go to the file menu, and \code{Change dir} to your Desktop or My Documents or something. Then: <>= desktop=getwd() options(repos="http://cran.us.r-project.org") install.packages("plotrix",destdir=desktop,lib=desktop) library(plotrix,lib=desktop) install.packages("gplots",destdir=desktop,lib=desktop) install.packages("gtools",destdir=desktop,lib=desktop) install.packages("gdata",destdir=desktop,lib=desktop) library(gtools,lib=desktop) library(gdata,lib=desktop) library(gplots,lib=desktop) @ } \item{\textbf{Dealing with times in \R\ (lab 2):} use the \code{times()} function in the \code{chron} library to convert character vectors or factor to times. e.g.: <>= install.packages("chron") @ <<>>= library(chron) timevec1 = c("11:00:00","11:25:30","15:30:20") times1 = times(timevec1) @ If you have times with no seconds component, use something like \code{timevec1=paste(timevec1,":00",sep="")} to add seconds before you try to convert. } \item{ \textbf{More on reshaping data:} <<>>= set.seed(1001) mydata = data.frame(indiv=rep(1:3,c(3,4,5)), sex=factor(c(rep("F",7),rep("M",5))), day=c(1:3,1:4,1:5),dist=runif(12)) @ Reshaping data (as Caro says) introduces \code{NA} values: <<>>= r1 = reshape(mydata,direction="wide",idvar="indiv",timevar="day", v.names="dist"); r1 @ Tabulate number of individual females in this format: <<>>= table(r1$sex) @ There may be a better way to do this but I haven't thought of it yet \ldots <<>>= splitdata = split.data.frame(mydata,mydata$indiv) firstlines = lapply(splitdata,function(x)x[1,]) recombined = do.call("rbind",firstlines) @ } \item{\textbf{setting default colors in \code{lattice} graphics:} To set all colors to gray scale: <<>>= library(lattice) trellis.par.set(canonical.theme(color=FALSE)) @ To set background color to white, leave foreground colors alone: <<>>= trellis.par.set(col.whitebg()) @ \end{enumerate} \end{document}