Р

R Programming Tutorial

R Programming Tutorial
You want to for beginner to expert R Programmer rapidly? R is one the best programming language for work with statistics, math and data science. This tutorial will help you to learn R and build your first machine learning model. Let's get started.

Compiling and Running R from the Command Line

The two ways to run R programs are: an R script, which is widely used and is most preferred and the second one is R CMD BATCH, it is not a commonly used command. We can call them directly from the command line or any other job scheduler.

You can conceivably call these commands from a shell built into the IDE and nowadays, the RStudio IDE does come with tools that enhance or manage the R script and R CMD BATCH functions.

source() function inside R is a good alternative to using the command line. This function can also call a script, but for using this function, you have to be inside the R environment.

R Language Built-in Data Sets

To list the data sets that are built-in with R, use the data() command, then find what you want, and use the name of the dataset in the data() function. Like data (functionname).

Show Data Sets in R

The question mark (?) could be used to ask for help for datasets.

To check for everything use summary ().

Plot () is also a function that is used to plot graphs.

Let's create a test script and run it. Create p1.R file and save it the home directory with the following contents:

Code Example:

 # Simple hello world code in R print("Hello World!") print("LinuxHint") print(5+6) 

Running Hello World

R Data Frames

For storing data in tables, we use a structure in R called a Data Frame. It is used to list vectors of equal length. For example, the following variable nm is a data frame containing three vectors x, y, z:

x = c(2, 3, 5) y = c("aa", "bb", "cc") z = c(TRUE, FALSE, TRUE) # nm is a data frame nm = data.frame(n, s, b) 

There is a concept called Built-inData Frames in R as well. mtcars is one such built-in data frame in R, which we will use as an example, for our better understanding. See the code below:

 > mtcars mpg cyl disp hp drat wt… Mazda RX4 21.0 6 160 110 3.90 2.62… bus RX4 Wag 21.0 6 160 110 3.90 2.88… Datsun 710 22.8 4 108 93 3.85 2.32… 

mtcars bulitin dataframe

The header is the top line of the table that contains the column names. Data rows are donated by each horizontal line; each line starts with the name of the row and then followed by the actual data. The data member of a row is termed as a cell.

We would enter the row and column coordinates in a single square bracket '[]' operator to retrieve data in a cell. To separate the coordinates, we use a comma. The order is essential. The coordinate begins with row then comma and then ends with the column. Cell value of 2nd row and 1st column is given as:

 > mtcars[2, 2] [1] 6 

We can also use row and column name instead of coordinates:

 > mtcars["Bus RX4", "mpg"] [1] 6 

nrow function is used to find the number of rows in the data frame.

 > nrow(mtcars) # number of data rows [1] 32 

ncol function is used to find the number of columns in a data frame.

 > ncol(mtcars) # number of columns [1] 11 

R Programming Loops

Under some conditions, we use loops when we want to automate some part of code, or we want to repeat a sequence of instructions.

For loop in R

If we want to print out these years information more than one time.

 print(paste("The year is", 2000)) "The year is 2000" print(paste("The year is", 2001)) "The year is 2001" print(paste("The year is", 2002)) "The year is 2002" print(paste("The year is", 2003)) "The year is 2003" print(paste("The year is", 2004)) "The year is 2004" print(paste("The year is", 2005)) "The year is 2005" 

Instead of repeating our statement again and again if we use for loop it will be much easier for us. Like this:

 for (year in c(2000,2001,2002,2003,2004,2005)) print(paste("The year is", year))  "The year is 2000" "The year is 2001" "The year is 2002" "The year is 2003" "The year is 2004" "The year is 2005" 

While Loop in R

 while (expression)  statement  

If the result of expression is TRUE, the body of the loop is entered. The statements inside the loop are performed, and the flow returns to assess the expression again. The loop will repeat itself until the expression evaluates to FALSE, in which case the loop exits.

Example of while Loop:

 # i is initially initialized to 0 i = 0 while (i<5)  print (i) i=i+1  Output: 0 1 2 3 4 

In the above while loop, the expression is i<5which measures to TRUE since 0 is less than 5. Hence, the body of the loop is executed, and i is output and incremented. It is important to increment i inside the loop, so it will somehow meet the condition at some point. In the next loop, the value of i is 1, and the loop continues. It will repeat itself until i equals 5 when the condition 5<5 reached loop will give FALSE and the while loop will exit.

R Functions

To create a function we use directive function (). Specifically, they are R objects of class function.

f <- function()  ##some piece of instructions  

Notably, functions could be passed to other functions as arguments and functions could be nested, to let you determine a function inside of another function.

Functions optionally can have some named arguments that have default values. If you don't want a default value, you can set its value to NULL.

Some facts about R Function arguments:

#Defining a Function: f <- function (x, y = 1, z = 2, s= NULL)   

Creating a logistic regression model with built-in data set

The glm() function is used in R to fit the logistic regression. glm() function is similar to the lm() but glm() has some additional parameters. Its format looks like this:

 glm(X~Z1+Z2+Z3, family=binomial (link=”logit”), data=mydata) 

X is dependent on the values of Z1, Z2, and Z3. Which means Z1, Z2 and Z3 are independent variables and X is the dependent Function involves extra parameter family and it has value binomial (link=“logit”) that means link function is logit and the probability distribution of regression model is binomial.

Suppose we have an example of student where he will get admission on the basis of two exam results. The data set contains the following items:

AppyMouse On-screen Trackpad and Mouse Pointer for Windows Tablets
Tablet users often miss the mouse pointer, especially when they are habitual to using the laptops. The touchscreen Smartphones and tablets come with m...
Middle mouse button not working in Windows 10
The middle mouse button helps you scroll through long webpages and screens with a lot of data. If that stops, well you will end up using the keyboard ...
How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...