Introduction :

The language was initially written by Ross Ihaka and Robert Gentleman at the Department of Statistics at the University of Auckland. Since its birth, a number of people have contributed to the package. R is open source statistical computing software as well as a language that suits best to a user. It is becoming popular day after day, because it is free, equipped with huge built-in functions and reliable help system. R environment allows a user to work standalone without connecting to a server.

Logo

How to install R: A Free Statistical Computing Software:

  1. Open any browser, say google chrome
  2. At the google search input, type CRAN
  3. The first post is The Comprehensive R Archive Network
  4. Double click the post
    1. R-4.2.2, at the time when I am downloading now, for Windows (32/64 bit) -> if you have a windows computer
    2. R for macOS -> if you have a MAC computer
  5. It will take you to a new page, where at the top you can see Download R 4.2.2 for Windows (76 megabytes, 64 bit)
  6. Double click it.
  7. This will download R software in your computer,it will take couple of minutes to download
  8. Save it somewhere in your computer or somewhere in your USB drive.
  9. Then double click the icon to install it in your computer, it will take couple of minutes.
  10. While installing, make sure that you check the box that says "creat desktop icon" somewhere in the process.
  11. When the installation process is completed you can see a big blue R in your desktop screen.
  12. Double click this big R, a new window pops up with a command prompt (>) sign. Ready to use.
  13. CONGRATULATIONS: you made it.

First Example :

Type the following code in a note pad, then copy it and paste at the R command prompt (>) and see the result a nice plot of a straight line shown on the left panel.

x <- -10:10
y <- 3*x + 5
plot(x,y, type = "l", main = "Plot of a straignt line", xlim = c(-10,10), ylim = c(-10,30), xlab="x-axis", ylab= "y-axis",col = "blue")
abline(h=0, lty=3)
abline(v=0, lty=3)

A graph

The Menu Bar

The menu bar in R is same as in most Window based programs. It has seven pull down menus, each is briefly described below.
File:
As usual the file is equipped with open, save, and print R documents, as well as the option of exiting the program. Besides, it has the option for opening existing and new script window. The options that begin with “load” (“Load Workspace and “Load History”) are options to open previously saved work. We can change the directory for the file menu as well.
Edit:
The edit menu contains as usual the cut, copy and paste, and select all items. In addition there is an option to “Clear console” which creates a blank workspace with only a command prompt but the R objects are still in working memory. The “Data editor” option allows to access the data editor, a spreadsheet like interface for manually editing data. The last option on the edit menu is “GUI preferences” which pops up the Rgui configuration editor, allowin to set options controlling the GUI, such as font size and background colors.
View:
It has 2 options, tool bar and status bar one can check to display.
Misc :
This menu is new here, cannot be seen in most of the window based programs. The first option is almost the same as ESC key in the keyboard, other options are not that very much used.
Packages:
This is the most important menu here, it helps us to load and install packages to the R system.
Windows:
The Windows menu provides options for cascading, and tiling windows. If there is more than one window open one can use the open Windows list on the bottom of this menu to access the different open windows.
Help:
The Help menu directs us to various sources of help.
The Toolbar:
Below the menu bar there is the toolbar. This provides quick access icons to the main features of the menu bar. If we scroll over the icons with the mouse it slowly gets rollover messages about the feature of each icon.

Use of R as a Calculator:


Copy the following commands and paste at the R console command prompt ( > )and enter to see the result.

Addition:
> 3 +2
Subtraction:
> 5-3
Multiplication:
> 4*6
Division:
> 8/2
Exponent
> 4^3 # 4 raised to third power
Some built-in Functions:
Square root function
sqrt(16)
sin, a trigonometrical function
sin(2*pi)
Natural logarithmic function
log(25)
logarithmic function with base = 10
log10(45)
exponential function
exp(2)

Arithmetical Operators

Operators Symbols
Addition +
Subtraction -
Multiplication *
Division /
Exponent ^

Logical and Relational Operators

Operators Symbols
And &
Or |
Not !
Equal to ==
Not equal to !=

Error
Copy paste the following command at the command prompt of the R console. Select and submit one by one, carefully notice the message at the message window.

> squareroot(8)
output: Error in squareroot(8) : could not find function "squareroot"
> sqrt 8
output: Error: unexpected numeric constant in "sqrt 8"
Correct form of the command :
> sqrt(8)
output: [1] 2.828427

Missing Values :
In many practical examples, some of the data elements will not be known and will therefore be assigned a missing value. The code for missing values in R is NA. This indicates that the value or element of the object is unknown. Any operation on an NA results in an NA.
The is.na() function can be used to check for missing values in an object.

value <- c(3,6,23,NA)
is.na(value)
any(is.na(value))
na.omit(value)

Assignment Operator (<-):
The assignment operator assigns the values on the right to the variable name on the left.

> x <- 3 +2
The folloing command displays what is stored in the variable name x
> x
output: [1] 5

Indefinite and Infinite Values
Indefinite and Infinite values (Inf, -Inf and NaN) can also be tested using the is.finite, is.infinite, is.nan and is.number functions in a similar way as shown above. These values come about usually from a division by zero or taking the log of zero.

> value1 <- 5/0
> value2 <- log(0)
> value3 <- 0/0
> cat("value1 = ",value1," value2 = ",value2, " value3 = ",value3,"\n")
output: value1 = Inf value2 = -Inf value3 = NaN

Concatenation :
The functions c ( ), rep ( ), and seq ( ) are used to create a vector. Copy, paste the following commands and submit one by one at the console:

Vector in Numeric mode
>x <- c (2,3, 5, 3, 7, 1)
>x
output: [1] 2 3 5 3 7 1
>y <- c (3, 5, 2, 7, 8, 12)
>y
output: [1] 3 5 2 7 8 12
The following command combines the vectors x and y
>z <- c(x,y)
>z
output: [1] 2 3 5 3 7 1 3 5 2 7 8 12
Vector in Numeric mode using operator :
>a <- 1:10
>a
output: [1] 1 2 3 4 5 6 7 8 9 10
Vector in Numeric mode using the function seq()
Sequence of numbers from 2 to 20 step by 2
>b1 <- seq(from = 2, to = 20, by = 2)
>b1
output: [1] 2 4 6 8 10 12 14 16 18 20
Short form of the same command
>b2 <- seq(2,20,2) # increasing sequence
>b2
>c <- seq(3,30, length = 10)
>c
output: [1] 3 6 9 12 15 18 21 24 27 30
decreasing sequence
>d <- seq(30, 12, -3)
>d
output: [1] 30 27 24 21 18 15 12
Vector in Numeric mode using the function rep()
The following command repeats the vector (2,4) 5 times
>e <- rep(c(2,4), 5)
>e
output: [1] 2 4 2 4 2 4 2 4 2 4