# R cheat sheet

```read.table() # read a text-format matrix into R as a data-frame
as.matrix() # coerce a data structure into matrix form
c() # create a column vector containing listed elements
dim() # Return the dimensionality of the indicated structure```
```[] #Use square brackets to indicate subset of data structure
#e.g. tmp[2:5,12:20] returns rows 2-5 of columns 12:20```
```{} #Use curly braces to group several different commands together before
#executing them.```
```for (i1 in c (1:10)) #Use the for statement to iterate through a column vector
#On each loop i1 will take on the next value in the
#specified vector, then the command following the for
#loop will be executed once.```
```barplot(c(), beside = T) #Plot a column vector as a parplot.
#Use ylim = c(min,max) to set min and max values for the
#y axis.```
```dist(matrix) #Returns the Euclidean distances between
#all rows of the matrix indicated. To inspect/visualize this
#it should be converted to a matrix using as.matrix()```
```image(dmatrix) #Plots a heat map of the supplied matrix. This is useful
#for visualizing distance matrices. By default the matrix is
#is plotted "backwards"; to see it in the canonical orientation
#you need to reverse-index the columns: image(dmatrix[,n:1]),
#where n=total number of columns```
```hclust(distance) #compute a hierarchical clustering based on the items in
#the supplied distance matrix.```
`cmdscale(distance) #compute a multidimensional scaling of the distance matrix`
```plot() #Generic plotting function. If applied to a 2-column matrix, generates
#a scatterplot. If applied to the results of hclust, generates a
#hierarchical cluster plot.```
```lines() #Add lines to the existing plots.
points() #Add points to the existing plots
text() #Add text to the existing plots```
`Some useful arguments for plotting functions:`
```type = "l" #Connects successive points with a line
type = "o" #Plots both points and lines
type = "n" #Sets up the axes but doesn't actually plot any data```
```xlab, ylab = "Axis labels" #Sets name for xaxis
xlim, ylim = c(x,y) # sets the minimum (x) and maximum (y) value
# for the x / y axes
lwd = x #Sets line width
lty = x #Sets line type (1 = solid, 2 = dashed, etc)
lcol = x #Sets line color```
`pch = x #Sets shape to use as point marker (use integer)`
```mtext(text, line, at, las) Plots test in the margines. text is a vector
#containing words to be plotted; line indicates
#how far from the edge it should be; at indicates
#where along the figure the text should appear;
#las indicates the orientation of the text.```