Saturday

How to code your own function in R programming language?

Advantage of a function

To automate certain tasks that you have to repeat over and over

Syntax

<object name> = function(<set of parameters>) {
    <set of R statements>
}


Examples

# Firstly, check the new function name
# so that this name is not the built-in functions in R
> getBasicStats
Error: object 'getBasicStats' not found

> getBasicStats = function(x) {
    stats = list()
    stats$length = length(x)  #$ used to reference an item
                              #in the list by its key name
    stats$mean = mean(x)
    stats$sd = sd(x)
    stats$median = median(x)
    unlist(stats)  #show stats as a horizontal vector
                   #instead of as a vertical list
}

> getBasicStats(c(1,2,3,4,5))
  length     mean       sd   median
5.000000 3.000000 1.581139 3.000000


/* */

Formula for the Parametric Representation of a Straight Line

Parametric representation of a straight line is also known as "convex combination of two points". Geometrically, the convex combination of two points means that a point is on the straight line between the two points.

Given three points p1=(x1,y1), p2=(x2,y2) and p3=(x3,y3),
p3 is a point between the straight line between p1 and p2 iff ∃t where 0<=t<=1 such that:

(i) x3=(t)x1+(1−t)x2
(ii) y3=(t)y1+(1−t)y2

Formula / Equation:

The point p3 equation is written as:

(iii) p3=(t)p1+(1−t)p2 iff ∃t where 0<=t<=1.

Visually:

                    t=0.3
   |--------------|------|
   x1             x3     x2


Examples:
 
Let x1=0 and x2=10.
Use formula (i).

When t=0.3,
then x3=(0.3)0+(0.7)10=7.

When t=0,
then x3=(0)0+(1)10=10.

When t=1,
then x3=(1)0+(0)10=0.

Therefore, you can see that the points p3 with 0<=t<=1 make up the straight line segment between the two given points p1 and p2.

Equivalent formula:
The point p3 formula (iii) p3=(t)p1+(1−t)p2 can also be written as:
(iv) p3=p2+t(p1−p2)
Perhaps, you recognize this as the parametric equation of a straight line through a point p2 with the direction vector (p1−p2) with t as the parameter.

Notes:
Analytical Proof = textually, consisting of words or text.
Geometric Proof = visually, consisting of figures or visuals.

Getting Started with R, RStudio, R Commander

Download R 3.0.2 64-bit for Windows from http://cran.stat.nus.edu.sg/ or http://cran.stat.nus.edu.sg/bin/windows/base/R-3.0.2-win.exe. R is an interpreter program for statistical computing and graphics. Install it.

Download RStudio Desktop IDE v0.98.501 from http://www.rstudio.com/ide/download/ or http://download1.rstudio.org/RStudio-0.98.501.exe. Install it.

Open RStudio program.
In the RStudio program, do the following:
* Toolbar > Create a new R script file
* Save the R script file and name it as rscripttest1.R.
* Type the following text in the rscriptest1.R file:
//
# this is a comment line
# get e^1 value
exp(1) # Ctrl+Enter to execute this line only, 2.718282    
13 %/% 4 # integer divide operation to get 3

# write log base e or ln (long) function using log command
y = log(exp(1))
y # output 1

# plot y = log(x) function 

# highlight four lines below and Ctrl+Enter
x = seq(-1,10,by=0.01)
y = log(x) #log base e 
plot(x,y,type='l') # l stands for line
abline(v=0,h=0) # draw two lines vertical=x=0 and horizontal=y=0
# view the plotted graph in the "Plots" window

//
* Go to the Console window, type "> help(abline)" and press "Enter" key to view help manual about abline command.
* In the RStudio Console window, type the following:
//
# c for concatenate is a built-in function
# c is used to combine elements, don't assign anything to variable named c using assignment symbol
> numbers = c(10,8,3,2,1,4,5,9,6,7)
> sort(numbers)
[1]  1  2  3  4  5  6  7  8  9 10
//

To install R Commander package, type the following in the RStudio Console window:
> install.packages("Rcmdr", dep=TRUE)
The R Commander program is a basic statistics GUI for R.
In the RStudio Console window, type the following to play with the R Commander program:
//
# to load R Commander library and open R Commander program
> library(Rcmdr)
# if you want to open R Commander version 2.0-3 program again, type "Commander()"

R Commander program will be opened.
Create a file named "DataCar.txt" in the C:\Users\username1\Documents folder. Save the following text to the "DataCar.txt" file:
--
"Make" "Model" "Cylinder" "Weight" "Mileage" "Type"
"1" "Honda" "Civic" "V4" 2170 33 "Sporty"
"2" "Chevrolet" "Beretta" "V4" 2655 26 "Compact"
"3" "Ford" "Escort" "V4" 2345 33 "Small"
"4" "Eagle" "Summit" "V4" 2560 33 "Small"
"5" "Volkswagen" "Jetta" "V4" 2330 26 "Small"
"6" "Buick" "Le Sabre" "V6" 3325 23 "Large"
"7" "Mitsbusihi" "Galant" "V4" 2745 25 "Compact"
"8" "Dodge" "Grand Caravan" "V6" 3735 18 "Van"
"9" "Chrysler" "New Yorker" "V6" 3450 22 "Medium"
"10" "Acura" "Legend" "V6" 3265 20 "Medium"
--
R Commander Menu > Data > Import data > from text file, clipboard or URL > Enter name for data set: Car > press OK button
Browse to the "DataCar.txt" file and hit Open button
R Commander toolbar > View data set. Close the Car data set window
R Commander Menu > Statistics > Summaries > Active data set
View the data set Car statistics such as frequency, min, median, mean, max in the RStudio Console window
//
Close the R Commander program by clicking the 'x' icon on the window title bar

RStudio Keyboard Shortcuts
Tab key = Code Completion
F1 = Go to Help or type help(<function name>) in the console window
F2 = Go to Function Definition, view the function source code
Ctrl+Shift+U = Extract Function, highlight first and then give the new function a name
Ctrl+I = reIndent Lines, example will be the two same symbols like two braces, two parentheses
Ctrl+Shift+/ = ReFlow Comment, this command will automatically re-wrap multiline roxygen comments that begin with #'
Ctrl+Shift+C = Comment/UnComment Lines
Ctrl+L = cLear Console in the console window
Ctrl+S = to save the R Script file

If a hater attacked your age and not the goodness of you

Whether young or old, I've always been known what endures. I've known the very idea of people that were all created equal and deserv...