Sei sulla pagina 1di 3

Notes: 1. Thisisbynomeansacomprehensivelist,asalargenumberofusefulfunctionshavebeenleftout,andnotall optionsforthefunctionslistedhavebeengiven.Thislistispurelyintentedtogiveaplacetobegin,asIremember howfrustratingitwastonotevenknowwhattostartlookingfor! 2. Typing?functionnameatthecommandlinebringsupahelpwindowforthefunctionnamelisted. 3. Assumeintheexamplesthatallvectorsandmatrices(visandmatis)havebeencreated. Command Operators General < = # Mathematical + * / ^ Logical/Relational == !

!= > >= < <= || | && & %*% Functions Example Result

RCheatsheet

Assignmentoperator(suggested) Assignmentoperator Comment Addition Subtraction Scalarmultiplication Divisionoperator Exponentiation

ans1<1 ans2=1+1 #Thisisacomment 2.5+ans3 ans32.5 2*3 6/2 2^3

1 2

5.5 0.5 6 3 8

Equals NotEqual GreaterThan GreaterThanorEqualTo LessThan LessThanorEqualTo Or Or(usewithvectorsandmatrices) And And(usewithvectorsandmatrices) Matrixmultiplication

ans3==3 ans3!=3 ans3>3 ans3>=3 ans3<3 ans3<=3 ans1==2||ans2==2 v2[v1==3|v1==4] ans1==2&&ans2==2 v2[v1==3&v1==4] mat1%*%mat1

TRUE FALSE FALSE TRUE FALSE TRUE TRUE {3,5} FALSE {NA}

sqrt exp log sum prod ceiling floor abs sin cos tan

Squareroot Exponentiation Naturallog Sum Product Smallestintegernumber Integerpartofanumber Absolutevalue Sine Cosine Tangent

sqrt(16) exp(1) log(2.718282) sum(2,3,4) prod(2,3,4) ceiling(2.1) floor(2.1) abs(0.2) sin(pi/2) cos(pi) tan(pi/4)

4 2.718282 1 9 24 2 2 0.2 1 1 1

table

Calculatefrequencycountsofavector table(v4)

135 [333]

Vector/MatrixFunctions Vectorcreationfunctions c Concatenate seq : rep Sequence Integersequence Repeat

v1<c(2,3,4) 2,3,4 v2<c(1,3,5) 1,3,5 v3<seq(from=2,to=10,by=2) 2,4,6,8,10 seq(from=2,to=4,length=5) 2.0,2.5,3.0,3.5,4.0 2:10 2,3,4,5,6,7,8,9,10 v4<rep(v2,3) 1,3,5,1,3,5,1,3,5

Combiningvectorstocreatematrices cbind rbind matrix as.data.frame Columnbind Rowbind Creatematrix Createdatasetfrommatrix mat1<cbind(v1,v2) mat2<rbind(v1,v2) matrix(0,nrow=2,ncol=3) A<as.data.frame(mat1)
2 1 3 3 4 5
2 1 0 0 3 3 0 0 4 5 0 0

Utilityfunctions [] [,]

2 3 4

1 3 5

Subscriptoperator(Vectors) Subscriptoperator(2D)

answer<v1[3] answer<mat1[1,1] answer<mat1[,1] answer<mat1[1,] answer<mat1[1,] answer<arr1[2,4,3] length(v4) sort(v4) order(v4) v4[v4.order] rev(v3) unique(v4)

4 2 2,1 2,3,4 114


3 3 4 5

Statistics

[,,] Subscriptoperator(3D) length Lengthofvector sort Sortavector order Indicestosortavector 1,4,7,2,5,8,3,6,9 Usefulforsortingmatrices rev Reverseorderofvector unique Listsuniqueobjectsinvectorormatrix max min pmax pmin mean median quantile var cor Maximumofvectorormatrix Minimumofvectorormatrix Parallelmaximumofvectors/matrices Parallelminimumofvectors/matrices Calculatesmeanofvectorormatrix Calculatesmedianofvectorormatrix Calculatequantilesrequested Calculatevarianceofvector Calculatescorrelationof2vectors

9 1,1,1,3,3,3,5,5,5

1,1,1,3,3,3,5,5,5 10,8,6,4,2 1,3,5

max(v4) 5 min(mat1) 1 pmax(v1,v2) 2,3,5 pmin(v1,v2) 1,3,4 mean(mat1) 3 median(v3) 6 quantile(1:5,probs=c(0,0.25,0.5,0.75,1)) 1,2,3,4,5 var(v3) 10 cor(v4,1:9) 0.3162

Distributions

d<dist>(x,<parameters>)densityatx p<dist>(x,<parameters>)CDFevaluatedatx q<dist>(x,<parameters>)inversecdf r<dist>(x,<parameters>)generatesnrandomnumbers

dunif(1.4,min=1,max=3) 0.5 pnorm(1.645,0,1) 0.95 qnorm(0.95,0,1) 1.645 rbeta(3,shape1=0.5,shape2=1) 0.175083,0.668609,0.009384 Defaults , 0,1 , 0,1 0,1 0,1 ,0 0,1 ##calculate5!usingaforloop ans<1 for(iin1:5){ans<ans*i} ans 120 ##Thresholdansat100 if(ans>100){ans2<100} else{ans2<ans} ans2

<dist> beta cauchy chisq exp f gamma lnorm Logis norm stab t unif

Distribution Beta Cauchy Chisquare Exponential F Gamma Lognormal Logistic Normal Stable Studentst Uniform

Parameters shape1,shape2 location,scale df df1,df2 shape mean,sd(of log) location,scale mean,sd index,skew df min,max

ForLoops for(iin<vector>){dostuff}

if/else if(<logicalvalue>){dostuff} else{dootherstuff}

100

Functions func.name<function(arg1,arg2,...){dostuff;return(ans)}

##Functiontodofactorial my.factorial<function(x){ if(!is.integer(x)) stop(xmustbeaninteger) ans<1 for(iin1:x){ans<ans*i} return(ans) } my.factorial(5) 120

Usefullinks: http://cran.rproject.org/doc/contrib/usingR2.pdf http://www.isds.duke.edu/computing/S/Snotes/Splus.html http://lib.stat.cmu.edu/S/cheatsheet

Potrebbero piacerti anche