### R

> source("osbs_14April15_data.R")
Browse> 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 linec()     concatenation / create vectorx <- c(3,5,7,9,2,11,13)xx,   x[2,] #2nd row  x[,3]  3rd columnx[2:5]x + 2; x * 2; x ^ 2; 2 ^ xx[x>3]  # elements greater than 3 (1:length(x))[x>3] # their indices x>3  # vector of true/falseas.numeric(x>3) #vector of 0s/1sx[-2] # all but 2nd element1:5seq    create sequencex=seq(0,2,by=0.01)  #OR length=10y=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 timesplotv / v; v ^ v      element-wise vector operationm1 %*% m2  matrix multiplication==     equalssort(x)length    #vector length cbindm1<-cbind(x,y);m1     # column-vise combine vectors of equal length vs rbind for row  t     transposematrix(c(1,3,2,5,-1,2,2,3,9),nrow=3) #reshape vector to matrixsolve(m)   #inverse matrixdiag(3)    # 3*3 diagonal matrixdiag(c(1,2,3))eigen(m2)            # eigen val and vectoreigen(m2)\$values For large matrices avoid computing the eigenvectors if they are not needed eigen(Sm, only.values = TRUE)\$valuesFunctionsx<-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 findercat      concat stringsnames      get/set name of an R objectedit()  edit in text editorView()hist()            hist(x, freq=false) #density/prob histnchar  #count number of charactersdata()  #list/load available datasetsstr    # R object to string       summary     summary of R objecthead    tail getwd()  # working directorysetwd()  read.csv('')rm(list=ls())  Clear all variablessubset(stateData, population<1000)==== states[states\$population<1000, ]subset(mtcars, select = -year) remove col&   |     ==== AND ORcond <- mtcars\$weight > 3.5mtcars\$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  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)`
` "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):