Rproj Mnl

From Biowiki
Jump to: navigation, search
R statistical package

help.start() - Help browser
help(function) - get help on function
example(function) - show examples of using function
getwd() - Get working directory
ls() - list objects
rm("obj") - remove object. rm(list=ls()) removes all objects.
length(obj) - object length
mode(obj) - object mode
attributes(obj) - show object attributes
  obj$attribute_name - get attribute with attribute_name
library() - list libraries
library(libraryName) - make libraryName active
system('cmd') - run system command

Basics:
  Logical not: !
  Assign a value: x <- 3
  Assign to a global variable within a function: x <<- 3
  Set vector to null: foo <- NULL

Reading data:
  Read table of data from file into a dataframe:
	 dmelData <- read.table("dmel_pf_maxscore.gff", header=FALSE)
  Acess column V2 of dataframe:
	  dmelData$V2
  Scan in specific columns of data file.  Denote by "" for string columns, and 0 for numeric columns:
	 scan(file="dmel_pf_maxscore.gff", what=list(chr=NULL,src="",type="",start=0,end=0,score=0), flush=TRUE)

String functions:
  Concatenate strings using null separator:
	 nfoldFile <- paste(dataDir,"dmel_pf_minscore.gff", sep="")

Vector functions:
c(x,y) - combine elements into a vector
duplicated(vec) - returns T/F if value in vec is duplicated
seq(start_value, end_value, increment, length) - generate sequence of numbers
  Also, vec <- start_value:end_value
rep(x, times = n) - replicate element n number of times and create vector
range(vec) - get min and max values in vector
sort(vec) - sort vector
order(vec) - return indices of sorted vector
which(vec) - return indices of elements which are True
unique(vec) - returns unique members

sum() - sum elements
cumsum() - cumulative sum
mean() - average
sd() - standard dev
sqrt()
round(), ceil(), floor()

Array/Matrix operations:
matrix(vec, nrow=?, ncol=?) - create matrix from data in vec (optional)
  mat[1,] - first row
  mat[,2] - second column
dim(mat) - matrix dimensions
t(mat) - transpose matrix mat
apply(mat, {1=rowwise,2=colwise}, function_name} - applies function to matrix mat
Fill an indicator matrix based on data in matrix mat: indicat[mat<=some_limit] <- 1
array() - create an array of given dimensions
	Ex: d <- array(c(a,b), dim=c(length(a), 2))
cbind(a,b), rbind(a,b) - bind the two vectors into a matrix (columnwise or rowwise)

list(x, y) - create a list, which can contain characters and numbers

Distributions:
cauchy
exp - exponential
gamma
geom - geometric
logis - logistical
normal
pois - Poisson
unif - uniform

Prefix:
d - density function
q - quantiles
p - probabilities
r - random numbers

Plotting:

plot(x, y) - plots data points
lines(x, y) - Adds lines thru data points

Histogram: hist(x) => Shows frequencies of data points in x
  Sets cell breakpoints and puts the cell right justified at each break point:
	  hist_ex <- hist(x, breaks=0:23, right=FALSE) 
  Set breakpoints, x axis range, specify column number, main title, x axis label and y axis label:
	 hist(dmelData$score, breaks=-370:10, xlim=c(-300,25), col=1, main="Pfold vs ncRNA", xlab="Score", ylab="Frequency (counts)")
  Can add a second column of data to plot:
	 hist(ncRNA$score, breaks=-210:10, xlim=c(-300,25), col=2, add=T)

sfun0=stepfun(time, vec) -> step plot
  plot.stepfun(sfun0)

Methods relating to user defined functions:
require(pkg_name) -> loads package pkg_name in function
search() - shows path list
debug(fnc) - debug function

Example function definition:
addTwo <- function(x)
{
 y <- x + 2
 return(y)
}

-- TWiki Guest - 05 Jul 2006