You are on page 1of 4

R has 5 basic or atomic classes of objects:

character, nymeric (real numbers), integer, complex and logical (True/False)

2 and 1/2 men


4-23
6-9

Datatypes in R:
Vector
Matrix
Array
List
Data Frame

Vector is a collection of different data elements that belong to an atomic type


5 basic atomic types are present or 5 classes of vector:
Logical: True/False
Integer: Whole Numbers
Numeric: Whole Numbers/Decimals
Complex: 4+3i
Characters: 'A', "Hey"
To comment anything in R, use #. Eg: #Hello! This is R

R studio
Single/Multiple line(s) of codes execution - Scripting area
Single line of codes execution - Console
Environment Area - Variable Operations
Install Packages/Help/Plots - 4th Workspace
vtr1 = c(TRUE,FALSE) = TRUE FALSE
vtr2 = c(3,3.12,TRUE) = 3.0, 3.12, 1.0
vtr3 = c(3,TRUE,"HELLO") = "3", "TRUE", "HELLO"

class(vtr3)

Matrix : R objects in which elements are arranged in a 2-d rectangular format


matrix(data , nrow , ncol , byrow , dimnames)
mtr = matrix(c(5:29),5,5)
[,1] [,2] [,3] [,4] [,5]
[1,] 5 10 15 20 25
[2,] 6 11 16 21 26
[3,] 7 12 17 22 27
[4,] 8 13 18 23 28
[5,] 9 14 19 24 29

m1 = 1:4
dim(m1) = c(2,2)
m1
[,1] [,2]
[1,] 1 3
[2,] 2 4

Arrays are R data objects which can store data in more than 2 dimensions
array(data , dim , dimnames)
arr = array(c(1:9),dim = c(3,3,4,2))

arr
, , 1, 1

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 2, 1

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 3, 1

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 4, 1

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 1, 2

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 2, 2

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 3, 2

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 4, 2

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

arr = array(c(1:6),dim = c(3,3,1,2))

, , 1, 1

[,1] [,2] [,3]


[1,] 1 4 1
[2,] 2 5 2
[3,] 3 6 3

, , 1, 2

[,1] [,2] [,3]


[1,] 1 4 1
[2,] 2 5 2
[3,] 3 6 3

Lists are R objects which contain elements of different datatypes - numbers,


strings, vector and another list inside it. Eg: list(data)

vtr = c(TRUE)
vtr12 = c(1,2.2)
vtr13 = c("Hi")
mylist1 = list(vtr,vtr12,vtr13)

mylist1
[[1]]
[1] TRUE

[[2]]
[1] 1.0 2.2

[[3]]
[1] "Hi"
Data Frame: A Table or a 2-d array in which each columns have values of one
variable and each row has one set of values in each column
If we have to load table from db or excel, load it in a dataframe. Eg:
data.frame(data)

vtr1 = c(1:5)
vtr2 = c("Emily","Paukeen","Emma", "Arknov","Alex")
vtr3 = c(3.2,2,4,6,9.2)
data.frame(vtr1,vtr2,vtr3)

data.frame(airmiles) #existing tables


data.frame(airquality) #existing tables

print(5%/%2) = 2 #Floor
print(5%%2) = 1 #Modulus

All 4 are acceptable:


20 = a (Equals)
a = 20 (Equals)
a <- 20 (Assigns)
20 -> a (Assigns)

value1 = c(TRUE,FALSE,TRUE,FALSE)
value2 = c(FALSE,TRUE,TRUE,FALSE)

Element wise AND

print(value1&value2)
FALSE FALSE TRUE FALSE

Element wise OR
print(value1 | value2)
TRUE TRUE TRUE FALSE

Logical AND (1st 2 values)


print(value1 && value2)
FALSE

Logical OR (1st value)


print(value1 || value2)
TRUE

You might also like