R


> setwd("/Users/morteza/Dropbox/academics/papers/presentations/neondsr/code/r")
> source("osbs_14April15_data.R")
Browse[2]> Q          # to exit debug mode

install.packages('knitr', dependencies = T)
library(knitr)


http://www.math.umaine.edu/~hiebeler/comp/matlabR.pdf
> help(boxplot)
USE RStudio


$ R
> q()     # To quit

------------------------------------------
 
<-      Assignment
#
      This is a comment line
c()     concatenation / create vector
x <- c(3,5,7,9,2,11,13)
x
x[1],   x[2,] #2nd row  x[,3]  3rd column
x[2:5]
x + 2; x * 2; x ^ 2; 2 ^ x
x[x>3]  # elements greater than 3
(1:length(x))[x>3] # their indices
x>3  # vector of true/false
as.numeric(x>3) #vector of 0s/1s
x[-2] # all but 2nd element
1:5
seq    create sequence
x=seq(0,2,by=0.01)  #OR length=10
y=2*sin(2*pi*(x-1/4))
plot(x,y)
plot(x,y,type='l')
plot(x,y,type='l', xlab='x-axis', ylab='y-axis', main='A plot of a sinusoid.')
rep(1,3)   repeat 1 3 times
plot
v / v; v ^ v      element-wise vector operation
m1 %*% m2  matrix multiplication
==     equals
sort(x)
length    #vector length

 cbind
m1<-cbind(x,y);m1     # column-vise combine vectors of equal length vs rbind for row
  t     transpose
matrix(c(1,3,2,5,-1,2,2,3,9),nrow=3) #reshape vector to matrix
solve(m)   #inverse matrix
diag(3)    # 3*3 diagonal matrix
diag(c(1,2,3))
eigen(m2)            # eigen val and vector
eigen(m2)$values For large matrices avoid computing the eigenvectors if they are not needed eigen(Sm, only.values = TRUE)$values

Functions

x<-seq(0.2,2,0.01)
y<-function (x) {y<-(log(x))^2-x*exp(-x^3)}
plot(x,y(x),type="l")
uniroot(y,lower=0.2,upper=1)$root #root finder
cat      concat strings
names      get/set name of an R object
edit()  edit in text editor
View()
hist()            hist(x, freq=false) #density/prob hist
nchar  #count number of characters
data()  #list/load available datasets
str    # R object to string
       summary     summary of R object
head    tail
 getwd()  # working directory
setwd() 
read.csv('')
rm(list=ls())  Clear all variables
subset(stateData, population<1000)
==== states[states$population<1000, ]
subset(mtcars, select = -year) remove col
&   |     ==== AND OR

cond <- mtcars$weight > 3.5
mtcars$weight_class <- ifelse(cond, 'heavy', mtcars$weight_class)

Data frame

Data frame is an array of columns of different types
Make<-c("Honda","Chevrolet","Ford","Eagle","Volkswagen","Buick","Mitsbusihi", "Dodge","Chrysler","Acura")
Model<-c("Civic","Beretta","Escort","Summit","Jetta","Le Sabre","Galant",  "Grand Caravan","New Yorker","Legend")
Cylinder<-c(rep("V4",5),"V6","V4",rep("V6",3))
Weight<-c(2170,2655,2345,2560,2330,3325,2745,3735,3450,3265)
Mileage<-c(33,26,33,33,26,23,25,18,22,20)
Type<-c("Sporty","Compact",rep("Small",3),"Large","Compact","Van",rep("Medium",2))

Car<-data.frame(Make,Model,Cylinder,Weight,Mileage,Type)     # combines the six vectors into a single data frame.

names(Car)      # "Make" "Model" "Cylinder" "Weight" "Mileage" "Type"
Car[1,]
Car$Mileage       # get millage column. ====  Car[,5]


> table(Car$Type)    #  table() command gives a frequency table:
> length(Car$Type)  # vector length
> table(Car$Make, Car$Type)  # cross tabulation
> i<-order(Car$Weight);i          # ORDER BY WEIGHT   
[1]  1  5  3  4  2  7 10  6  9  8
> Car[i,] 

> data1<-edit(data.frame())   # edit an empty frame in built in spreadsheet editor

> hist(Car$Mileage)
> hist(Mileage, freq=F)    # if probability instead of frequency is desired


> data(faithful)
> attach(faithful)   # attach() allows to reference variables in fuel.frame without the cumbersome fuel.frame$ prefix.
> names(faithful)
[1] "eruptions" "waiting"
> hist(eruptions, seq(1.6, 5.2, 0.2), prob=T)
> lines(density(eruptions, bw=0.1))
> rug(eruptions, side=1)

> boxplot(Weight)                # usual vertical boxplot
> boxplot(Weight, horizontal=T)  # horizontal boxplot
> rug(Weight, side=2)

Boxplot is more useful when comparing grouped data. For example, side-by-side boxplots of weights grouped by vehicle types are shown below:
> boxplot(Weight ~Type) ; title("Weight by Vehicle Types")

> plot(Weight)
> plot(Weight, Mileage, main="Weight vs. Mileage")  # a scatterplot with Weight on the x-axis and Mileage on the y-axis.


RMD

R markdown which can include R code and display their results








Subpages (1): SciDB-R
Comments