U. of Arizona

**General**`q()`

— quit the program.`control-c`

— stop R if it's doing something you don't want.`apropos('some topic')`

— find out about some topic (must be quoted).`help(command)`

— find out about some command.`?command`

— same as above.

**Importing Data**`data.frame()`

— create a statistical object by typing it in at the prompt.age <- c(25,30,56,22,17,9) gender <- c("m","f","m","m","f","f") weight <- c(160,110,220,150,90,100) major <- c('ling','comm','comm','ling','ling','ling','ling') g <- data.frame(age,gender,weight,major)

`fix()`

— alter a statistical object by typing it in with the R editor.g <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0), major=character(0)) fix(g)

`read.table()`

— read in a text file you have created.g <- read.table('gender.txt',header=T)

**Objects**`ls()`

— list currently loaded objects.`rm(object)`

— delete a currently loaded object.`summary(object)`

— find out about some object.

**Accessing a Dataframe**`g$gender`

— access a column by name.`g[[1]]`

— access a column as a list.`g[,1]`

— access a column.`g[1,]`

— access a row.`g[4,2]`

— access a cell.`g[3:6,1:2]`

— access a block.`g[g$gender=='f',]`

— subsetting rows.g[g$gender=='f',] g[g$weight < 160,] g[g$gender=='m' & g$weight < 160,]

`attach(object)`

— make the parts of some currently loaded object directly available, e.g.`g$gender`

is available as`gender`

.`detach(object)`

— remove the attached object.`search()`

— examine what objects are attached.

**Basic Statistics**`mean()`

— mean.`sd()`

— standard deviation.`var()`

— variance.- In general, all mathematical functions are available for
**vectorized**calculations.

**Manipulating Parts of Dataframes**`tapply()`

— apply some function by factors to a dataframe.tapply(weight,gender,mean) tapply(weight,gender,length)

`t()`

— transpose a dataframe.`transform()`

— alter a dataframe in some way.transform(g,age=age+weight)

`aggregate()`

— aggregate within some dataframe.aggregate(g,list(gender),length)

`as.factor()`

— convert a list/vector of numbers into a factor.`is.factor()`

— test if something is a factor.`cut(vector,levels)`

— break a vector of numbers into a factor with n levels.tapply(age,cut(weight,2),mean)

**Plotting**`plot()`

— generic plotting function; lots and lots of options.plot(age ~ gender) plot(age,weight) plot(age ~ cut(weight,3))

`barplot()`

— makes a barplot, without whiskers, lots and lots of options.barplot(tapply(age,gender,mean),xlab="Gender", col=c('sienna','violet'),names.arg=c('female','male'))

`colors()`

— lists all available named colors.`hist()`

— makes a histogram from a list of numbers`interaction.plot()`

— creates an interaction plot for two factors.interaction.plot(gender,major,age)

**Anova**`aov()`

— creates an anova object which can be examined for the usual information with`summary()`

. The tricky part is the anova formula.`aov(x ~ f1 * f2 + Error(sub/f1))`

—`x`

is a vector representing a response,`f1`

is a within-items factor,`f2`

is a within-subjects factor, and`sub`

is the subjects factor. The`*`

includes interactions.`aov(x ~ f1 * f2 + Error(itm/f2))`

— here everything is the same except that`itm`

is the items factor.sn <- read.table('twosn.txt',header=T) summary(sn) attach(sn) tapply(response,list(sonority,size),mean) summary(aov(response ~ sonority * size + Error(subject/(sonority * size)))) summary(aov(response ~ sonority * size + Error(item))) interaction.plot(sonority,size,response) interaction.plot(sonority,size,response,ylim=c(1,7)) with(sn[sn$size=='three',],summary(aov(response ~ sonority + Error(subject/sonority))))

**Regression**`lm()`

— does a linear regression. One also needs to enter a formula, but it's simpler.`lm(x ~ y + z + ...)`

—`x`

,`y`

,`z`

, etc. are vectors of numbers (not factors).attach(g) sumary(lm(age ~ weight))

**Gotchas****Input file format**textfile, tabs, missing fields, column issues.**Dataframe**columns/rows, vector vs. factor.**Anova**within-items factors vs. within-subjects factors.

**Scripting**- A simple function:
sjj <- function (x) { paste("Hey, ", x, ", this is tone sandhi.", sep="") }

- A more complex function:
ip.to.ps <- function(filename,f1,f2,r) { postscript(filename) interaction.plot(f1,f2,r, trace.label = deparse(substitute(f1)), xlab = deparse(substitute(f2)), ylab = paste("mean of",deparse(substitute(r)))) dev.off() }

- A simple function:
**Links**- http://www.r-project.org Main R site. R can be downloaded from here, lots and lots of documentation, tutorials, etc.
- http://www.statmethods.net One useful R reference site.
- http://pidgin.ucsd.edu/mailman/listinfo/r-lang R for linguists home page.
- http://www.pallier.org/ressources/tpr/tpR.html General R tutorial by Christophe Pallier (in French).
- http://www.pallier.org/ressources/stats_with_R/stats_with_R.pdf Christophe Pallier's tutorial on doing Anova with R (in French).
- http://dingo.sbs.arizona.edu/~hammond/Rwkshp/ URL for workshop materials.