2 Program
2 Program
A) Variables:
Variables are nothing but reserved memory locations to store values. This means that, when
create a variable you reserve some space in memory. A variable provides us with named
storage that our programs can manipulate. A variable in R can store an atomic vector, group
of atomic vectors or a combination of many Robjects. A valid variable name consists of
letters, numbers and the dot or underline characters. The variable name starts with a letter or
the dot not followed by a number.
name<-"John"
age<-40
name#output"John"
age # output 40
The variables can be assigned values using leftward, rightward and equal to operator. The
values of the variables can be printed using print()
# Assignment using equal operator.
var.1=c(0,1,2,3)
# Assignment using leftward operator.
var.2<- c("learn","R")
# Assignment using rightward operator.
c(TRUE,1)->var.3
print(var.1)
print(var.2)
print(var.3)
B) Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
In R, variables do not need to be declared with any particular type, and can even change
type after they have been set:
Example
my_var<-30#my_varistypeofnumeric
my_var <- "Sally" # my_var is now of type character
R has a variety of data types and object classes.
Basic Data Types
We can use the class() function to check the data type of a variable:
#numeric
x<-10.5
class(x)
#integer
x<-1000L
class(x)
#complex
x<-9i+3
class(x)
#character/string
x<-"Risexciting"
class(x)
#logical/boolean
x<-TRUE
class(x)
R-objects.
Vectors
Lists
Matrices
Arrays
Factors
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.
print(apple)
print(class(apple))
o/p: "character"
Lists
A list in R can contain many different data types inside it. A list is a collection of data which
is ordered and changeable.
Example
#Listofstrings
thislist<-list("apple","banana","cherry")
#Printthelist
thislist
Access Lists
You can access the list items by referring to its index number, inside brackets. The first item
has index 1, the second item has index 2, and so on:
Example
thislist<-list("apple","banana","cherry")
thislist[1]
Change Item Value
Example
thislist<-list("apple","banana","cherry")
thislist[1]<-"blackcurrant"
#Printtheupdatedlist
thislist
List Length
To find out how many items a list has, use the length() function:
Example
thislist<-list("apple","banana","cherry")
length(thislist)
Add List Items
To add an item to the end of the list, use the append() function:
Example
thislist<-list("apple","banana","cherry")
append(thislist, "orange")
Remove List Items
You can also remove list items. The following example creates a new, updated list without an
"apple" item:
Example
newlist<-thislist[-1]
#Printthenewlist
newlist
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range,
by using the : operator:
Example
(thislist)[2:5]
Matrices
A matrix can be created with the matrix() function. Specify the nrow and ncol parameters to
get the amount of rows and columns:
Example
#Createamatrix
thismatrix<-matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)
#Printthematrix
thismatrix
2) # Create a matrix.
M =matrix( c('a','a','b','c','b','a'),nrow=2,ncol=3,byrow= TRUE)
print(M)
o/p: [,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"
Arrays
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. In
the below example we create an array with two elements which are 3x3 matrices each.
# Create an array.
a <- array(c('green','yellow'),dim= c(3,3,2))
print(a)
o/p:
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
Factors
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','green','yellow','red','red','red','green')
factor_apple<- factor(apple_colors)
print(factor_apple)
print(nlevels(factor_apple))
C) Operators:
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
10 + 5
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Miscellaneous operators
R Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
^ Exponent x^y
%% Modulus (Remainder from x %% y
division)
R Assignment Operators
Example
my_var<-3
my_var<<-3
3->my_var
3->>my_var
== Equal x == y
!= Not equal x != y
Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are
TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
Example:
Arithmetic Operators
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
Relational Operators
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
Logical Operators
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
Assignment Operators
v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 = c(3,1,TRUE,2+3i)
print(v1)
print(v2)
print(v3)