.chapter1<-function(i=0){ " i Chapter 1: R basics i Value Assignment/Functions - ------------------------------------- -- ------------------------------------- 1 Download and install R 21 Assignment without defining it 2 Launch/quit R, one line for this course 22 Three ways to assign a value 3 Comment line and comment paragraph 23 Upper and down arrow keys 4 3 ways to assign a value/values 24 One benefit of -> assignment 5 Use the up and down arrow keys 25 String variable 6 R is case sensitive 26 typeof() function 7 Normal operations: +, -, /, ^ (power) 27 x<-c(1,10) 8 listing function ls() 28 .nLetterFunctions 9 remove a variable or several variables 29 scan() function 10 remove all variables 30 x=1:100 11 use meaningful variable names 31 seq() function 12 current working directory 32 read.table('clipboard') 13 head() and tail() functions 33 help(read.table) 14 mean() and sd() 34 readline() function 15 put several commands on one line 35 mean() and sd() functions 16 The simplest function 36 other embedded functions 17 use .nLetterFunctions() 37 Precision of R 18 use the help() function 38 .Machine 19 Videos 20 Links Example #1:>.c1 # see the above list Example #2:>.c1(1) # see the first explanation ";.zchapter1(i)} .n1chapter<-38 .zchapter1<-function(i){ if(i==0){ print(.c1) }else{ .printEachQ(1,i,.n1chapter) } } .c1<-.chapter1 .C1EXPLAIN1<-"From where to download R //////////////////////////////// To install R, we have the following steps: Step 1: go to http://r-project.org Step 2: click \"CRAN\" on the left Step 3: choose a server nearby Step 4: choose appropriate type such as Windows, Mac Step 5: download \"Base\" /////////////////////////////// " .C1EXPLAIN2<-"launch/quit R, one line for this course //////////////////////////////// There are several ways to quit R ------------------------------- 1) type q() 2) click \"File\" -> \"Quit\" 3) click the red x on top right Note: after type q(), you will be asked \"Keep the objectives\". If you want to keep your variables or functions, answer \"yes\". Otherwise, no. One-line R code for this course --------------------------------- source(\"http://datayyy.com/fmr.R\") /////////////////////////////// " .C1EXPLAIN3<-"Comment line and comment paragraph //////////////////////////////// A good comment would make your program more readable Method 1:Anything after # will be a command > pv=10 # pv is for present value Method II: a pair of double quotation marks //////////////////////////////// " .C1EXPLAIN4<-"3 ways to assign a value/values and how to show its value //////////////////////////////// Three methods: Method I: >pv=100 Method II: >r<-0.1 Method III:>10->n To show the value of a variable, type its name > pv=100 > pv [1] 100 /////////////////////////////// " .C1EXPLAIN5<-"Use up and down arrow keys to recall the previous commands //////////////////////////////// We could use up and down arrow keys to recall our previous commands. Thus, we could save time by recalling the previous command and modify it a little bit. > pv= 100 > r = 0.1 > n = 2 > fv_f(pv,r,n) [1] 121 > pv= 200 > fv_f(pv,r,n) [1] 242 > /////////////////////////////// " .C1EXPLAIN6<-"R is case sensitive //////////////////////////////// Case sensitive means that pv is different from PV > pv=100 > PV Error: object 'PV' not found > /////////////////////////////// " .C1EXPLAIN7<-"Normal operations: +, -, /, ^ (power) //////////////////////////////// The normal simple operations will remain the same > 1+5/2-4^2 [1] -12.5 /////////////////////////////// " .C1EXPLAIN8<-"listing function ls() //////////////////////////////// To list all variable for functions, we use the ls() Example #1: Assume that we have nothing, see below. > ls() character(0) > Example #2: > pv<-100 > r<-0.1 > n<-5 > ls() [1] \"n\" \"pv\" \"r\" > /////////////////////////////// " .C1EXPLAIN9<-"remove a variable or several variables //////////////////////////////// 1) To remove a variable, we use the rm() function >rm(pv) 2) To remove two variables such as pv and r, >rm(pv,r) //////////////////////////////// " .C1EXPLAIN10<-"remove a variable or several variables or all //////////////////////////////// To remove all variables, we issue the following codes >rm(list=ls()) //////////////////////////////// " .C1EXPLAIN11<-"using meaningful variables names //////////////////////////////// When using meaningful names, the program is easy to understand. >pv=100 >r=0.1 >n=2 > pv*(1+r)^n [1] 121 /////////////////////////////// " .C1EXPLAIN12<-"current working directory //////////////////////////////// To find out the current working directory, we use getwd() >getwd() To change the current working directory, we use setwd() >setwd(\"c:/temp\") /////////////////////////////// " .C1EXPLAIN13<-"head() and tail() functions //////////////////////////////// > set.seed(123) > x=rnorm(100) > head(x) [1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 > tail(x,2) [1] -0.2357004 -1.0264209 /////////////////////////////// " .C1EXPLAIN14<-"mean() for mean and sd() for standard deviation //////////////////////////////// > set.seed(123) > x=rnorm(100) > mean(x) [1] 0.09040591 sd() for estimating standard deviation > set.seed(123) > ret<-rnorm(100) > sd(ret) [1] 0.9128159 //////////////////////////////// " .C1EXPLAIN15<-"put several command lines together //////////////////////////////// We use semicolon to separate them > x<-10;y<-20 # several commands together /////////////////////////////// " .C1EXPLAIN16<-"the simplest function //////////////////////////////// Here is one line function for the present value Example #1: double any input value dd=function(x)2*x dd is the function name function is the keyword x in the parentheses is the input value 2*x is the output put > dd(2.3) [1] 4.6 Example #2: >pv_function<-function(fv,r,n)fv/(1+r)^n >pv_funtion(100,0.1,1) [1] 90.90909 /////////////////////////////// " .C1EXPLAIN17<-"use .nLetterFunction() to show all n-letter functions //////////////////////////////// To see how to use this function, just type its name >.nLetterFunctions To find out all embedded functions with just 3-letter long > .nLetterFunctions(3) [1] \"$<-\" \"%*%\" \"%/%\" \"%o%\" \"%x%\" \".gt\" \":::\" \"@<-\" \"[<-\" \"<<-\" \"abs\" \"acf\" [13] \"AIC\" \"all\" \"any\" \"aov\" \"Arg\" \"ave\" \"BIC\" \"bmp\" \"BOD\" \"box\" \"bxp\" \"cat\" [25] \"ccf\" \"co2\" \"CO2\" \"col\" \"cor\" \"cos\" \"cov\" \"cut\" \"det\" \"dim\" \"dir\" \"end\" [37] \"exp\" \"fft\" \"fix\" \"for\" \"get\" \"glm\" \"hat\" \"hcl\" \"hsv\" \"IQR\" \"lag\" \"lcm\" [49] \"log\" \"mad\" \"Map\" \"max\" \"min\" \"Mod\" \"new\" \"nlm\" \"nls\" \"npk\" \"Ops\" \"par\" [61] \"pdf\" \"pie\" \"png\" \"ppr\" \"raw\" \"rep\" \"rev\" \"rgb\" \"rle\" \"row\" \"rug\" \"seq\" [73] \"sin\" \"SSD\" \"stl\" \"str\" \"sub\" \"sum\" \"svd\" \"svg\" \"tan\" \"tar\" \"try\" \"tsp\" [85] \"unz\" \"url\" \"var\" \"x11\" \"X11\" \"xor\" \"zip\" /////////////////////////////// " .C1EXPLAIN18<-"Using help() function //////////////////////////////// For example, we want to know more about the mean() function > help(mean) /////////////////////////////// " .C1EXPLAIN19<-"Videos //////////////////////////////// YouTube: how to download and install R (6m39s) [for windows) https://www.youtube.com/watch?v=ZoPJGmpYJzw YouTube: Programming in R - Getting Started - Installing R and RStudio on a Mac (5m59s) https://www.youtube.com/watch?v=Ywj6yNfc5nM Why couldn't I launch RStudio? ------------------------------------- For Mac users https://www.youtube.com/watch?v=20XiBheVsck For Windows users (control + RStudio) https://www.youtube.com/watch?v=t7DStkaOULs /////////////////////////////// " # https://www.youtube.com/watch?v=bm3OdMZT9u8 .C1EXPLAIN20<-"Links //////////////////////////////// R download: http://r-project.org RStudio download https://posit.co/download/rstudio-desktop/ My website http://datayyy.com/webs/R.shtml An Introduction to R http://datayyy.com/doc_pdf/R-intro.pdf R Language Definition http://datayyy.com/doc_pdf/R-lang.pdf One programming language: http://canisius.edu/~yany/doc/one_language.pdf R, Python or SAS: Which one should you learn first? https://www.edvancer.in/r-python-or-sas-which-one-should-you-learn-first/ http://canisius.edu/~yany/doc/whichonefirst.pdf SAS vs. R (vs. Python) https://www.analyticsvidhya.com/blog/2014/03/sas-vs-vs-python-tool-learn/ http://canisius.edu/~yany/doc/comparion_02.pdf My slides for week 1 http://datayyy.com/doc_pdf/fmur1.pdf or http://canisius.edu/~yany/doc/fmur1.pdf Old syllabus http://datayyy.com/doc_pdf/syllabus694.pdf chapter 1 http://datayyy.com/doc_pdf/chapter01.pdf /////////////////////////////// " .C1EXPLAIN21<-"Assigning a value without defining it first /////////////////////////////// For many programming languages, programmers have to define a variable first. For R, we could simply assign a value, such x=100 y=500 ///////////////////////////////// " .C1EXPLAIN22<-"Three ways to assign a value /////////////////////////////// pv<- 100 r=0.1 n-> 5 ///////////////////////////////// " .C1EXPLAIN23<-"Upper-arrow and down-arrow keys /////////////////////////////// We typing commands, we can use the up-arrow key to recall the previous commands. Thus, by using the up-arrow and down-arrow keys, we can save some of our typing time ///////////////////////////////// " .C1EXPLAIN24<-"One benefit of -> assignment /////////////////////////////// Assume we type the following 100*(1+0.1)^2 If we want to assign the above result to a value, we can use the upper-arrow key to recall the command, then add ->a, shown below. 100*(1+0.1)^2 ->a ///////////////////////////////// " .C1EXPLAIN25<-"String variable /////////////////////////////// We could use a pair double quotation marks or a pair single quotation marks to circle a string. firstName<-\"John\" companyName<-'International Business Machine' ///////////////////////////////// " .C1EXPLAIN26<-"typeof() function /////////////////////////////// > firstName<-'John' > typeof(firstName) [1] \"character\" > pv<-10.56 > typeof(pv) [1] \"double\" > n<-5.6 > typeof(n) [1] \"double\" > n2<-as.integer(n) > typeof(n2) [1] \"integer\" ///////////////////////////////// " .C1EXPLAIN27<-"Combine function: x<-c(1,10) /////////////////////////////// x<-c(1,10) ///////////////////////////////// " .C1EXPLAIN28<-".nLetterFunctions /////////////////////////////// The function would show all function with n-character long. .nf is the short-form for the .nLetterFunctions > .nf(3) [1] \"$<-\" \"%*%\" \"%/%\" \"%@%\" \"%+%\" \"%>%\" \"%>%\" \"%>%\" \"%>%\" \"%>%\" \"%>%\" \"%o%\" \"%x%\" [14] \":::\" \"@<-\" \"[<-\" \"<<-\" \"abs\" \"acf\" \"aes\" \"AIC\" \"all\" \"any\" \"aov\" \"Arg\" \"ave\" [27] \"BIC\" \"bmp\" \"BOD\" \"box\" \"bxp\" \"cat\" \"ccf\" \"co2\" \"CO2\" \"col\" \"cor\" \"cos\" \"cov\" [40] \"cut\" \"det\" \"dim\" \"dir\" \"do_\" \"end\" \"exp\" \"fft\" \"fix\" \"for\" \"get\" \"glm\" \"hat\" [53] \"hcl\" \"hsv\" \"IQR\" \"lag\" \"lag\" \"lcm\" \"log\" \"lst\" \"lst\" \"mad\" \"map\" \"Map\" \"max\" [66] \"min\" \"Mod\" \"mpg\" \"new\" \"nlm\" \"nls\" \"npk\" \"nth\" \"Ops\" \"par\" \"pdf\" \"pie\" \"png\" [79] \"ppr\" \"quo\" \"quo\" \"raw\" \"rel\" \"rep\" \"rev\" \"rgb\" \"rle\" \"row\" \"rug\" \"seq\" \"sin\" [92] \"sql\" \"src\" \"SSD\" \"stl\" \"str\" \"sub\" \"sum\" \"svd\" \"svg\" \"sym\" \"sym\" \"tan\" \"tar\" [105] \"tbl\" \"try\" \"tsp\" \"unz\" \"url\" \"var\" \"who\" \"x11\" \"X11\" \"xml\" \"xor\" \"zip\" > ///////////////////////////////// " .C1EXPLAIN29<-"scan() function /////////////////////////////// When inputting from our keyboard, we can use scan() function x<-scan() ///////////////////////////////// " .C1EXPLAIN30<-"x<-1:100 /////////////////////////////// When the incremental value is 1, we could use x<-n1:n2 x<-1:100 y<-100:1 ///////////////////////////////// " .C1EXPLAIN31<-"seq() function /////////////////////////////// x<-seq(1,22,by=1) y<-seq(1,22,by=2) ///////////////////////////////// " .C1EXPLAIN32<-"read.table('clipboard') /////////////////////////////// Step 1: we highlight a data set from Notepad, Excel, Word, etc. Step 2: issue the following line x<-read.table('clipboard') Example 1 2 3 3 4 5 5 6 6 ///////////////////////////////// " .C1EXPLAIN33<-"help(read.table) /////////////////////////////// To find more about the read.table function, we issue the following code help(read.table) ///////////////////////////////// " .C1EXPLAIN34<-"readline() function /////////////////////////////// > x<-readline() this is great > x [1] \"this is great\" > ///////////////////////////////// " .C1EXPLAIN35<-" 15 mean() and sd() functions /////////////////////////////// x-1:10 mean(x) sd(x) ///////////////////////////////// " .C1EXPLAIN36<-"other embedded functions /////////////////////////////// you use apropos() function or .nLetterFuntinos() ///////////////////////////////// " .C1EXPLAIN37<-"Precision of R /////////////////////////////// > .Machine$double.eps [1] 2.220446e-16 > ///////////////////////////////// " .C1EXPLAIN38<-".Machine variable /////////////////////////////// The .Machine is a variable describe 18 parameters of your R based on your machine. > length(.Machine) [1] 18 > .Machine[1:5] $double.eps [1] 2.220446e-16 $double.neg.eps [1] 1.110223e-16 $double.xmin [1] 2.225074e-308 $double.xmax [1] 1.797693e+308 $double.base [1] 2 ///////////////////////////////// "