The core of R is an interpreted computer language which allows branching and looping as well as modular programming using functions.
R was initially written by Ross Ihaka and Robert Gentleman at the Department of Statistics of the University of Auckland in Auckland, New Zealand.
Features of R
As stated earlier, R is a programming language and software environment for statistical analysis, graphics representation and reporting. The following are the important features of R −
R is a well-developed, simple and effective programming language which includes conditionals, loops, user defined recursive functions and input and output facilities.
R has an effective data handling and storage facility,
R provides a suite of operators for calculations on arrays, lists, vectors and matrices.
R provides a large, coherent and integrated collection of tools for data analysis.
R provides graphical facilities for data analysis and display either directly at the computer or printing at the papers.
As a conclusion, R is world’s most widely used statistics programming language.
It is the #1 choice of data scientists and supported by a vibrant and talented community of contributors.
Single comment is written using # in the beginning of the statement as follows −
R does not support multi-line comments but you can perform a trick which is something as follows −
if(FALSE) {"This is a demo for multi-line comments and it should be put inside either a single OR double quote" }
myString <- ello="" p="" world="">print ( myString)
Though above comments will be executed by R interpreter, they will not interfere with your actual program. You should put such comments inside, either single or double quote.
Generally, while doing programming in any programming language, you need to use various variables to store
various information. Variables are nothing but reserved memory locations to store values. This means that, when you create a variable you reserve some space in memory.
In contrast to other programming languages like C and java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable.So R is called a dynamically typed language, which means that we can change a variable’s data type of the same variable again and again when using it in a program
There are many types of R-objects. The frequently used ones are −
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
The simplest of these objects is the vector object and there are six data types of these atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors.
Logical ,Numeric ,Integer ,Complex ,Character ,Raw
Vectors
When you want to create vector with more than one element, you should use c() function which means to combine the elements into a vector.
# Create a vector.
apple <- c="" green="" p="" red="" yellow="">print(apple)
# Get the class of the vector.
print(class(apple))
A list is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it.
# Create a list.
list1 <- c="" list="" p="" sin="">
# Print the list.
print(list1)
A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix function.
# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
While matrices are confined to two dimensions, arrays can be of any number of dimensions. The array function takes a dim attribute which creates the required number of dimension.
# Create an array.
a <- array="" c="" dim="c(3,3,2))</p" green="" yellow="">print(a)
Factors are the r-objects which are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. The labels are always character irrespective of whether it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.
Factors are created using the factor() function.The nlevels functions gives the count of levels.
# Create a vector.
apple_colors <- c="" green="" p="" red="" yellow="">
# Create a factor object.
factor_apple <- apple_colors="" factor="" p="">
# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))
Data Frames
Data frames are tabular data objects. Unlike a matrix in data frame each column can contain different modes of data. The first column can be numeric while the second column can be character and third column can be logical. It is a list of vectors of equal length.
Data Frames are created using the data.frame() function.
# Create the data frame.
BMI <- span="" style="white-space: pre;"> ->->->->->->->
data.frame( gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
) print(BMI)
The values of the variables can be printed using print() or cat()function. The cat() function combines multiple items into a continuous print output.
# Assignment using equal operator.
var.1 = c(0,1,2,3)
# Assignment using leftward operator.
var.2 <- c="" learn="" nbsp="" p="">
# Assignment using rightward operator.
c(TRUE,1) -> var.3
print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")
Finding Variables
To know all the variables currently available in the workspace we use the ls() function. Also the ls() function can use patterns to match the variable names.
print(ls())
Deleting Variables
Variables can be deleted by using the rm() function. Below we delete the variable var.3. On printing the value of the variable error is thrown.
rm(var.3)
print(var.3)
All the variables can be deleted by using the rm() and ls() function together.
rm(list = ls())
print(ls())
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides following types of operators.
Types of Operators
We have the following types of operators in R programming −
Arithmetic Operators + − * / %% %/%
Relational Operators > < == <= >= !=
Logical Operators & | !
Assignment Operators
Miscellaneous Operators
Built-in Function
Simple examples of in-built functions are seq(), mean(), max(), sum(x) and paste(...) etc
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))
# Find mean of numbers from 25 to 82.
print(mean(25:82))
# Find sum of numbers frm 41 to 68.
print(sum(41:68))
# Create a function to print squares of numbers in sequence.
new.function <- 1:a="" a="" b="" for="" function="" i="" in="" nbsp="" p="" print="">
# Call the function new.function supplying 6 as an argument.
new.function(6)
->->