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.
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)
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.
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)
Operators | Symbols |
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Exponent | ^ |
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