0% found this document useful (0 votes)
25 views

Lec3 - Variables and Datatypes - Vectors and Lists

Uploaded by

tkpatil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lec3 - Variables and Datatypes - Vectors and Lists

Uploaded by

tkpatil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data science for Engineers

Variables and datatypes in R

Variables and data types: Vectors and Lists 1


Data science for Engineers

In this lecture
 Variables
 Basic data types
 R objects
◦ Vectors
◦ Lists

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 2


Data science for Engineers

Variables

Variables and data types: Vectors and Lists 3


Data science for Engineers

Variables
Variable names - Rules Examples

 Allowed characters Correct naming:


are Alphanumeric, > b2 =7
‘_’ and ‘.’
> Manoj_GDPL = “Scientist”
> Manoj.GDPL = “Scientist”
 Always start with
alphabets
Wrong naming :
> 2b = 7
 No special
characters like Error: unexpected input in
!,@,#,$,…. “2b "

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 4


Data science for Engineers

Predefined constants
Constant Symbol in R

Pi pi

letters a,b,c,…….x,y,z

LETTERS A,B,,…..,X,Y,Z

Months in a month.name,
year month.abb

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 5


Data science for Engineers

Data types

Variables and data types: Vectors and Lists 6


Data science for Engineers

Basic data types


Basic data types Values

Logical TRUE and FALSE

Integer Set of all integers, Z

Numeric Set of all real numbers

Complex Set of complex numbers

“a”,”b”,”c”,….,”x”,”y”,”z”,”@”,”#”,”$”, “”,”*”,
Character
“1”,”2”,… etc..

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 7


Data science for Engineers

Basic data types


TASK ACTION SYNTAX/EXAMPLE

Find data type of object use command “typeof()” Syntax: typeof(object)

Verify if object is of a use prefix “is.” before Syntax:


is.data_type(object)
certain datatype datatype as command.
Example : is.integer()

Coerce or convert data use prefix “as.” before Syntax:


as.data_type(object)
type of object to another datatype as command. Example :as.logical()

Note : Not all coercions are possible and if attempted will return “NA” as output
Sample Codes

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 8


Data science for Engineers

Basic objects

Object Values

Vector Ordered collection of same data types

List Ordered collection of objects

Data frame Generic tabular object

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 9


Data science for Engineers

Vectors

Variables and data types: Vectors and Lists 10


Data science for Engineers

Vectors
• Vector : an ordered collection of basic data types of given
length

• All the elements of a vector must be of same data type

Code Console Output

# Vectors Example

X = c(2.3,4.5,6.7,8.9)

print(X)

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 11


Data science for Engineers

Lists

Variables and data types: Vectors and Lists 12


Data science for Engineers

Lists in R: create a list


• List : a generic object consisting of an ordered collection of
objects
• A list could consist of a numeric vector, a logical value, a matrix,
a complex vector, a character array, a function, and so on

Code Console Output

# List Example : Employee details


ID = c(1,2,3,4)
emp.name =c("Man","Rag","Sha","Din")
num.emp = 4
emp.list = list(ID, emp.name,num.emp)
print(emp.list)

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 13


Data science for Engineers

Accessing components (by names)


• All the components of a list can be named
• These components can be accessed using the given names

Code Console Output

# Continue after first 4 lines of R


# code from previous example
emp.list = list(“Id”= ID,
“Names” = emp.name,
“Total staff”=num.emp)
print(emp.list$Names)

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 14


Data science for Engineers

Accessing components (indices)


To access top level components, use double slicing operator “ [[ ]]”
and for lower/inner level components use “[ ]” along with “[[ ]]”

Code Console Output

# continuing from previous


# code
print(emp.list[[1]])
print(emp.list[[2]])
print(emp.list[[1]][1])
print(emp.list[[2]][1])

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 15


Data science for Engineers

Manipulating lists
A list can be modified by accessing components & replacing them

Code Console Output

# continuing from previous code


emp.list['Total staff']=5
emp.list[[2]][5]="Nir“
emp.list[[1]][5]=5
print(emp.list)

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 16


Data science for Engineers

Concatenation of lists
Two lists can be concatenated using the concatenation function,
c(list1, list2)

Code Console Output

# continuing from previous code


emp.ages = list(“ages” =
c(23,48,54,30,32))
emp.list= c(emp.list , emp.ages)
print(emp.list)

Variables and data types: Vectors and Lists NPTEL NOC18-CS28 17

You might also like