Skip to content

R notes

January 10, 2013

My Personal R Cheat Sheet

If someone sees this post. I welcome any suggestions or questions.

Progress Bar:

  • matrix(1:6, nrow  = 2)[c(1,1,2,2,2),] # Repeat first rows twice, second row 3 times.

Progress Bar:

  • total <- 20
    # create progress bar
    pb <- txtProgressBar(min = 0, max = total, style = 3)
    for(i in 1:total){
       # update progress bar
       setTxtProgressBar(pb, i)

Some Random Functions:

  • rm(list=ls())  #Remove everything (function, variables, etc.).
  • example(function) #Run the examples given in the help file of the function.
  • str() #Check complete data type and information, more detailed than is().
  • sapply()# Apply a function over a list or vector.
  • hist2d()#Histogram on 2 variables.
  • scatterplot3d() #3d plots


  • strsplit(): split a string.
  • substr(string,1,1): substring.

Data Type: list

  • names(): Get a string of the names of the list elements
  • vector("list", length = 7): Create a list of length 7

Data Type: dataframe

  • colnames(): Returns strings of column names.
  • myframe[1:3]: Returns the first three columns of data frame “my frame”. Still a data frame. (consider “subset()”)
  • nrow(): Returns number of rows.
  • ncol(): Number of columns.
  • subset(): very useful in selecting certain rows and/or columns of the data frame.


  • &: point to point logic and
  • &&: entire vector logic and
  • a %in% b: a is a vector, b is a set. Check EACH entry of a to see if it is in b, yes->TRUE, no->FALSE. Returns a logic vector with the same length of a.

Graphics(basic, see post “lattice” for more):

R opens “devices” for graphs. Default device is on screen. Multiple devices can exist at the same time. Several pages, or several graphs on a same page in one device can be created. A device to create pdf file can be called by:

  • pdf("filename.pdf")

Some graphic functions are “high level” functions, which starts a graph in the current device. “low level” functions modify existing graphs. Graphs can also be modified by arguments in the call of high level functions. A high level function:

  • plot(y,type = "p")

If “y” is a data frame, then scatter plot of all pairs of variables will be plotted. plot treats different variable types differently.

  • curve(function name): plots the graph of the function. For example curve(log).
  • matplot(dataframe): plots individual scatter plots on one graph.

boxplot(colname1 ~ colname2, data = dataframe, col = “light grey”, boxwex = 0.5): plots multiple boxplots of colnames1, according to different values of colnames2. boxwex controls the width of boxes.

Adjusting margins:

#mar is the margin around each individual plot, oma is the
outer margin area around all the plots.
  • par(mfrow=c(2,3), mar=c(5,5,5,3), oma=c(3,3,5,3)) #Bottom,Left, Top, Right.

Add Title to page:
#the “line” value tells how far away from the figures the title should be placed.

  • mtext("All my plots",outer=TRUE,line=1)

Graphic Arguments:

Arguments such as lty = “dashed”  can be set as device parameter using par(), which is persistent in all plots drawn in the current device, or as function argument, which temporarily affects the current plot only (and overwrites the device argument temporarily).

      • colors(): List of all colors

Categorical Data:

      • ftable(3 way table):  prints 3 way contingency table in a better way.
      • structable(3 way table): Same as above

Data Frame:

      • a = data.frame(matrix(NA,nrow = n, ncol = m)): Create empty data frame
      • names(a) = c("dd",...): Change dataframe names
      • Sort data.frame by different columns: 
      • mydata = data.frame(x = 1:10, y = rep(5,10), z = rnorm(10))
      • mydata = mydata[order(data$y,data$x),]#sort the data by y first, then by x.

Visual Progress Bar (so you know when your for LOOP will end…):

total <- 20
# create progress bar
pb <- txtProgressBar(min = 0, max = total, style = 3)
for(i in 1:total){
   # update progress bar
   setTxtProgressBar(pb, i)
   #Add whatever expressions you want to evaluate.


as.formula(paste(“y~”,paste(listoffactors,collapse=”+”))) # create dynamic formulas.

An example of  functions: with and par.setting:

wd.cur  is a data frame that have columns surface, patient, days,obs.


stripplot(surface ~ patient,

    subset(wd.cur, days %in% c(0,1)),

    group = obs[,drop = TRUE],

    par.settings = simpleTheme(col=c(rep("red",3),rep("blue",5)),

     pch=c(1:3,1:5), cex=1.2, lwd=1),

    auto.key = list(space = "right"),

    main = "Initial measurements: Surface",

    xlab = "patients"



Install Packages from CRAN:

install.packages('RMySQL', repos='')

Time Series

- Date-Time:
- as.Date set dates

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: