R Notes

getwd to get current working directory (analogous to pwd)
setwd to set current working directory
options(digits=3) will print numbers with 3 decimal digits
runif(20) – create vector of 20 uniformly distributed random numbers
dir – to list files in a directory
dir.create – to create a directory
source(‘myscript.R’) – to execute a script
install.packages(Rwave) – to install Rwave package
update.packages() to update packages
installed.packages() shows details of installed packages
help(package=’Rwave’) will list functions in package Rwave
library(Rwave) – to load Rwave package
library() – shows what packages you have saved in your library
search() shows which packages are loaded and ready for use
.libPaths() shows where your library is located e.g. C:/Program Files/R/R-3.1.1/library
sink(‘filename’) will direct output to filename
sink() will restore output to screen
pdf(figure.pdf) save figure as pdf
png(figure.png) save figure as png
length(x) returns length of x
dim(x) returns dimensions of x (should be matrix or array)
str(x) tells structure of x
class(x) tells what class is x type of
mode(x) tells how x is stored
names(x) gives names of components (e.g., columns) of x
5 main types of collections in R – vectors, matrices, arrays, data frames and lists
c to create vectors
matrix to create matrix
array to create array (matrix with more than 2 dimensions) e.g., array(1:24,c(2,3,4)) creates 2x3x4 array
data frame is a matrix whose columns can have different data types df=data.frame(col1,col2,col3)
df[1:2] will return first 2 columns of data frame
the names argument of data.frame ctor can be used to give names to the various columns. We can then index by column names instead of having to use integers to index.
use table function to cross tabulate 2 columns of a data frame e.g., table(df$col1,df$col2)
use attach and detach to avoid having to prefix name of data frame again and again
also see with which provides similar functionality
Nominal and ordinal variables in R are called factors (think of enums in other languages). Both are created using factor function. Using ordered=TRUE in the ctor tells R if you are creating ordinal variables (whose values can be ordered). The order can also be specified using the levels param e.g.,


is basically saying that data is an enum where {male=1,female=2}
Lists are well lists. they are indexed using double square brackets [[ and ]]
to read data from a flat file use read.table


header indicates whether the first record in file contains the column headings
sep is separator used to separate columns
row.names specifies the columns that can be used to ID a record (sort of like PK in SQL)
dev.new() similar to figure in matlab (create new figure)
more commends: dev.next, dev.prev, dev.set, dev.off
par(mfrow=c(m,n)) to create a figure with m by n plots in it
graphics.off() to close all graphs
plot commands starts a new graph
lines command adds to current plot
abline can be used to draw vertical and horizontal reference lines on a plaot
x%%y same as x%y in C# 5%%2 = 1
x%/%y returns quotient 5%/%2=2
!= not equal to
== equal to
!x not x
x|y x OR y
x&y x AND y
isTRUE(x) test if x is TRUE
cut allows you to divide a numeric range into intervals returning a factor. These intervals can then be used with tapply to do binning.
Sys.Date() returns today’s date
date() returns current date and time
order() to sort data
which gives indices of a vector that are TRUE
sample() function can be used to do sampling
diff(x,lag=1) will return difference of successive elements
to apply a function on a particular column, you can use transform() e.g.


seq(to,from,by) e.g. seq(0,10,2) returns 0,2,4,6,8,10
lapply and sapply apply a function over a list
to measure cross correlation between signals use

ccf(x, y, lag.max = NULL, type = c("correlation", "covariance"), plot = TRUE, na.action = na.fail, ...)

to measure just the correlation, use cor
see this answer to find the time at which maxima occurs in cross correlation

Find_Max_CCF<- function(a,b)
 d <- ccf(a, b, plot = FALSE)
 cor = d$acf[,,1] // gives cross correlation function
 lag = d$lag[,,1] // gives vector of time lags
 res = data.frame(cor,lag)
 res_max = res[which.max(res$cor),] // gives time lag at which max cc occurs

to apply a circular shift to a vector, i came across this code:

<pre>circshift <- function(a, sz) {
    if (is.null(a)) return(a)
    if (is.vector(a) && length(sz) == 1) {
        n <- length(a)
        s <- sz %% n
        if (s > 0) a <- a[c((s+1):n, 1:s)]

    } else if (is.matrix(a) && length(sz) == 2) {
        n <- nrow(a); m <- ncol(a)
	    s1 <- sz[1] %% n
	    s2 <- sz[2] %% m
        i1 <- if (s1 > 0) c((s1+1):n, 1:s1) else c(1:n)
        i2 <- if (s2 > 0) c((s2+1):m, 1:s2) else c(1:m)
	    a <- a[i1, i2]

    } else
        stop("Length of 'sz' must be equal to the no. of dimensions of 'a'.")


to convert a list of vectors into a matrix use this:

M=do.call(rbind,lapply(data$M,function(x) extract_features_internal(x,fs)))

to extract filename from full path use basename(fullPath)
to compute average correlation between columns of a matrix use:
C = cor(M) # computes correlation between columns of M
correlation = mean(C[lower.tri(C)]) # get average correlation

use proc.time() to get time
See this link http://www.ltrr.arizona.edu/~dmeko/notes_6.pdf to really understand what goes behind the scenes when you call spec.pgram or spec.ar or spectrum
Use drop to Delete the dimensions of an array which have only one level.
To find indices of top N elements in a list or vector use this:

order(R, decreasing=TRUE)[1:N]

To filter a data frame on values of its column use this:

obj = subset(table,column_name==class)

above will return those rows of table where column_name equals class

This entry was posted in Software. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s