You are on page 1of 20

Objects in R

Objects in R
Variable
 Is an object with a single value. It is the simplest object in R
 Can be assigned an expression involving other variables
 Examples: x = 3, or x = b + c where b = 2 and c = 6
Vector
 Is a collection of elements of the same data type
 Examples: {1, 2, 3}, {“Amar”, “Akbar”, “Anthony”}, {10.4, 7.8,
9.1}
Sequence
 Is a vector made of numbers
 Have a fixed pattern
 Examples: {1, 4, 7}, {5.1, 6.2, 7.3}, {1 + 2i, 2 + 3i, 3 + 4i}
Objects in R
Array

 Is a multi-dimensional series of data

 Each element has the same data type

Matrix

 Is a two-dimensional array of the same data type

Factor

 Is like a vector where distinct objects are stored as levels

 Can take any data type


Objects in R
List

 It is like a vector

 Each component of a list can be a different object

 It can contain components of different data types

Data Frame

 Used for storing data tables

 Is a list of equal length vectors

 Has a matrix like structure


Vectors
Creating Vectors

 Function c() is used to create a vector

 Examples:
>x<-c(1,3,4,6)

> x

[1] 1 3 4 6

> x<-c("Amar","Akbar","Anthony")

> x

[1] "Amar" "Akbar" "Anthony"


Attributes
> x<-c(1,2,3)
> mode(x)
[1] "numeric"
> length(x)
[1] 3
> typeof (x)
[1] double
> class (x)
[1] numeric
Replication
 Function rep() can be used to replicate a vector

 Usage: rep({vector}, each =…, times = …) to


replicate a vector

 Examples:
> x<-c(1,3,5)
> rep(x,2)
[1] 1 3 5 1 3 5
> rep(x,each=2,times=3)
[1] 1 1 3 3 5 5 1 1 3 3 5 5 1 1 3 3 5 5
> rep(x,c(2,4,3))
[1] 1 1 3 3 3 3 5 5 5
Sorting

 Function sort() is used to orders vector arguments

 Usage: sort({vector}, decreasing = ...) to sort a


vector

 Examples:
> x<-c(1,3,5,2,1,3,7,3,2)
> sort(x)
[1] 1 1 2 2 3 3 3 5 7
> sort(x,decreasing = True)
Error in sort(x, decreasing = True) : object
'True' not found
> sort(x,decreasing = TRUE)
[1] 7 5 3 3 3 2 2 1 1
[

Concatenation
 Function paste() can be used to concatenate
two or more vectors

 Examples:
> x<-c(1,2,3)
> y<-c("A","B","C")
> paste(x,y)
[1] "1 A" "2 B" "3 C" > x<-3
> paste("The value of x is", x)
> paste(x,y,sep="&") [1] "The value of x is 3"

[1] "1&A" "2&B" "3&C"


> paste(x,y,sep="-")
[1] "1-A" "2-B" "3-C"
> z<-c("Ram","Shyam")
> paste(x,z)
[1] "1 Ram" "2 Shyam" "3 Ram"
Sequences
Sequences
 Function seq() can used to create a sequence
 Usage: seq(from=.., to=.., by=.., length=..) to create a
sequence
> x<-seq(1:5)
 Examples: > x
[1] 1 2 3 4 5
> x<-seq(from=1,to=10,by=2)
> x
[1] 1 3 5 7 9
> x<-seq(from=1,by=5,length=10)
> x
[1] 1 6 11 16 21 26 31 36 41 46

 Function c() can also used to create a sequence


 Usage: c(from:to) to create a sequence
 Examples: x<-c(1:5)
x
 [1] 1 2 3 4 5
> x<-c(1:3-1)
x
 [1] 0 1 2
> x<-c(2+1:5)
x
 [1] 3 4 5 6 7
> x<-c(3*2:5+1)
x
 [1] 7 10 13 16
Arrays
Creating an Array
(Method 1)
 Define a vector using function c() and then
declare the vector as an array by including the
function dim() [this assigns a dimension vector]

 Examples:
Creating an Array
(Method 2)

Function array() can be used to perform


both steps in a single assignment

 Examples:
Retrieving Elements and
Subsections
 Elements of an array can be retrieved by
referencing its position, or the index
Exercises
1. Creating Vectors
1.a) Create a numeric vector: x={20,30,40}. Test if it is a vector using
is.vector function. Check its type and mode.

1.b) Replicate the vector created above

➡ i Two times

➡ ii Repeating every number twice

➡ Both i and ii

➡ Repeating the first number 5 times and the third number twice

➡ Sort the vector in ascending order

➡ Sort the vector in descending order

1.c) Create a character vector: Specializations={“Marketing”,


”Finance”, “HR”, “IB”, “Analytics”).
2. Creating Sequences

Create the following sequences:

 2.a) From 5 to 20 in increments of 0.25

 2.b) From 5 to 20 in increments of 0.3

 2.c) From 5 for a length of 10 in increments of 0.5

 2.d) The sequence 3.5, 5.5, 7.5, 9.5 [Hint: use c()]:
3. Creating Arrays

3.a) Create a 3 dimensional array: values 3 to 14,


dimensions 2x3x2. Test if array. Check its type, mode
and class

3.b) Create a 3 dimensional array: values 6 to 28


increments of 2, dimensions 2x3x2. Test if array

3.c) Return sub-section of array from 3b) associated


with the third value of the second dimension

You might also like