R is programming language which is used for statistical analysis, graphic representations and reporting.
R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently being developed by the R Development Core Team. One of the interesting fact is that this programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka).
At very core of R programming language there lies an interpreted computer language which allows branching and looping as well as modular programming.
R is open source programming language GNU-style copy left, and an official part of the GNU project called GNU S.
Following are the key features of R programming:
● R is simple and effective programming language
● R is effective in data handling and storage
● R provides excellent mechanism for calculation of different matrices.
● R provides variety of tools for data analysis
● R provides graphical facilities for data analysis
Rscript is the R interpreter which is used to execute the R program scripts in command prompt.
R program scripts have .R extension. Here is the sample script:
$ Rscript test.R
R-Objects are the objects which are assigned to the variables and the data type of the R-object becomes the data type of the variable. Different types of R-Objects are:
● Lists
● Vectors
● Arrays
● Matrices
● Factors
● Data Frames
Vector object is the simplest R-Object.
There are six data types also termed as classes of vectors. Following are the data types:
● Logical
● Numeric
● Integer
● Complex
● Character
● Raw
Here is the sample code for these six data types:
● Logical:
val <- FALSE print(class(val))
val <- 27.5 print(class(val))
val <- 21L print(class(val))
val <- 27+3i print(class(val))
val <- "READ" print(class(val))
val <- charToRaw("Testfry") print(class(val))
C() function is used to combine the elements into a vector.
Sample code:
apple <- c('red','green','yellow') print(apple)
Matrix is a two-dimensional data set which can created using matrix function.
Sample code:
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) print(M)
Factors are the R-Objects that are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. Factor() function can be used.
Sample code:
factor_app <- factor(colors)
These are the tabular data objects in R programming. Data.frame() function can be used to create data frames.
Yes, in R programming variable name can start with dot(.) i.e. it is valid variable name. The only thing that need to be taken into consideration with writing the variable name is that the dot shouldn't followed by the number.
Sample valid variable name:
.abc
.3abc
In R programming there are multiple ways to assign the values to the variables. Following are the ways to assign the values:
● Assignment using equal operator
● Assignment using leftward operator
● Assignment using rightward operator
Sample code for assignment using equal operator:
var.1 = c(0,13,24,344,35)
var.1 <- c("lean","G")
c(TRUE,1) -> var.1
R is called as dynamically typed language which means we can change the data type of same variable again and again in same program.
Sample code:
var_2 <- "abc" var_2 <- 14
ls() function is use to determine all the variables which are currently available in the workspace.
No, the variables start with dot won't be listed using ls() function.
The variables start with dot can be listed using "all.names = TRUE" argument to ls() function.
Sample code:
print(ls(all.name = TRUE))
Variables can be deleted using rm() function.
We can delete all the variables at same time using rm() and ls() functions.
Sample code:
rm(list = ls())
Following are the different types of operators in R programming:
● Arithmetic Operators
● Assignment Operators
● Logical Operators
● Relational Operators
● Miscellaneous Operators
Following are the different arithmetic operators:
+ : Adds two vectors
- : Subtraction of two vectors
* : Multiplies both the vectors
/ : Division of the vectors
%% : Give the remainder
^ : Exponential
Following are the different assignment operators:
<- , = , << - : These are the left assignment operators
-> , ->> : These are the right assignment operators
Following are the different logical operators:
&, && : Logical AND operator
|,|| : Logical OR operator
! : Logical NOT operator
Following are the different Relational operators:
> : Greater than logical operator
< : Less than logical operator
== : Equal to logical operator
<= : Less than equal to logical operator
>= : Greater than equal to logical operator
!= : Not equal to logical operator
Following are the different Miscellaneous operators:
: Colon operator. It creates the series of numbers in sequence for a vector.
%in% : This operator is used to identify if an element belongs to a vector.
%*% : This operator is used to multiply a matrix with its transpose.
s <- c( 2,5.5,3) t <- c(4, 3, 4) print(s*t)
Here is the output:
[1] 8.0 16.5 12.0