SlideShare a Scribd company logo
1 of 28
Download to read offline
R Programming
Sakthi Dasan Sekar
http://shakthydoss.com 1
Data structures
a) Vector
b) Matrix
c) Array
d) Data frame
e) List
http://shakthydoss.com 2
Data structure
Vectors are one-dimensional arrays
a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
a is numeric vector,
b is a character vector, and
c is a logical vector
http://shakthydoss.com 3
Data structure
Scalars are one-element vectors.
f <- 3
g <- "US"
h <- TRUE.
They’re used to hold constants.
http://shakthydoss.com 4
Data structure
The colon operator :
a <- c(1:5)
is equivalent to
a <- c(1,2, 3, 4, 5)
http://shakthydoss.com 5
Data structure
Vector
You can refer to elements of a vector using a numeric vector of positions within
brackets.
Example
vec <- c(“a”, “b”, “c”, “d”, “e”, ”f”)
vec[1] # will return the first element in the vector
vec[c(2,4)] # will return the 2nd and 4th element in the vector.
http://shakthydoss.com 6
Data structure
Matrices
Matrix are two-dimensional data structure in R.
Elements in matrix should have same mode (numeric, character, or logical).
Matrices are created with the matrix() function.
vector <- c(1,2,3,4)
foo <- matrix(vector, nrow=2, ncol=2)
http://shakthydoss.com 7
Data structure
Matrices byrow (optional parameter)
byrow=TRUE, matrix elements are filled by row wise.
byrow=FALSE, matrix elements are filled by column wise.
foo <- matrix(vector, nrow=2, ncol=2, byrow = TRUE)
foo <- matrix(vector, nrow=2, ncol=2, byrow = FALSE)
http://shakthydoss.com 8
Data structure
Matrix element can be accessed by subscript and brackets
Example
mat <- matrix(c(1:4), nrow=2,ncol = 2)
mat[1,] # returns first row in the matrix.
mat[2,] # returns second row in the matrix.
mat[,1] # returns first column in the matrix.
mat[,2] # returns second column in the matrix.
mat[1,2] # return element at first row of second column.
http://shakthydoss.com 9
Data structure
Array
Arrays are similar to matrices but can have more than two dimensions
Arrays are created with the array() function.
array(vector, dimensions, dimnames)
a <- matrix(c(1,1,1,1) , 2, 2)
b <- matrix(c(2,2,2,2) , 2, 2)
foo <- array(c(a,b), c(2,2,2))
http://shakthydoss.com 10
Data structure
Array
array elements can be accessed in the same way a matrices.
foo[1,,] # returns all elements in first dimension
foo[2,,] # returns all element in second dimension
foo[2,1,] # returns only first row element in second dimension
http://shakthydoss.com 11
Data structure
Data frame
Data frames are the most commonly used data structure in R.
Data frame is more like general matrix but its columns can contain different
modes of data (numeric, character, etc.)
A data frame is created with the data.frame() function
data.frame(col1, col2, col3,..)
name <- c( “joe” , “jhon” , “Nancy” )
sex <- c(“M”, “M”, “F”)
age <- c(27,26,26)
foo <- data.frame(name,sex,age)
http://shakthydoss.com 12
Data structure
Data frame
Accessing data frame elements can be straight forward. Element can be
accessed by column names.
Example
foo$name # retruns name vector in the data frame
foo$age # retuns age vector in the data frame
foo$age[2] # retuns second element of age vector in the data frame
http://shakthydoss.com 13
Data structure
Factors
Categorical variables in R are called factors.
Status (poor, improved, excellent) and Gender (Male, Female) are good
example of an categorical variables.
Factor are created using factor() function.
gender <- c(“Male", “Female“, “Female”, “Male”)
status <- c(“Poor”, “Improved” “Excellent”, “Poor” , “Excellent”)
factor_gender <- factor(gender) # factor_genter has two levels called Male and Female
factor_status <- factor(status) # factor_status has three levels called Poor, Improved and
Excellent.
http://shakthydoss.com 14
Data structure
List
Lists are the most complex data structure in R
List may contain a combination of vectors, matrices, data frames, and even
other lists.
You create a list using the list() function
vec <- c(1,2,3,4)
mat <- matrix(vec,2,2)
foo <- list(vec, mat)
http://shakthydoss.com 15
Data Import/Export
Import Excel File
Quite frequently, the sample data is in Excel format, and needs to be
imported into R prior to use.
library(gdata) # load gdata package
help(read.xls) # documentation
mydata = read.xls("mydata.xls") # read from first sheet
http://shakthydoss.com 16
Data Import/Export
Import Excel File
Alternate package XLConnect
library(XLConnect)
wk = loadWorkbook("mydata.xls")
df = readWorksheet(wk, sheet="Sheet1")
http://shakthydoss.com 17
Data Import/Export
Import Minitab File
If the data file is in Minitab Portable Worksheet format, it can be
opened with the function read.mtp from the foreign package. It returns
a list of components in the Minitab worksheet.
library(foreign) # load the foreign package
help(read.mtp) # documentation
mydata = read.mtp("mydata.mtp") # read from .mtp file
http://shakthydoss.com 18
Data Import/Export
Import Table File
A data table can resides in a text file. The cells inside the table are separated
by blank characters. Here is an example of a table with 4 rows and 3
columns.
100 a1 b1
200 a2 b2
300 a3 b3
400 a4 b4
help(read.table) #documentation
mydata = read.table("mydata.txt")
http://shakthydoss.com 19
Data Import/Export
Import CSV File
The sample data can also be in comma separated values (CSV) format.
Each cell inside such data file is separated by a special character, which
usually is a comma.
help(read.csv) #documentation
mydata = read.csv("mydata.csv", sep=",")
http://shakthydoss.com 20
Data Import/Export
Export Table file
help(write.table) #documentation
write.table(mydata, "c:/mydata.txt", sep="t")
Export Excel file
library(xlsx)
help(write.xlsx) #documentation
write.xlsx(mydata, "c:/mydata.xlsx")
http://shakthydoss.com 21
Data Import/Export
Export CSV file
help(write.csv)
write.csv(mydate, file = "mydata.csv")
Avoid writing the headers
write.csv(mydata, file = "mydata.csv", row.names=FALSE)
http://shakthydoss.com 22
Data Import/Export
Knowledge Check
http://shakthydoss.com 23
Data Import/Export
Every individual data value has a data type that tells us what sort of
value it is.
A. TRUE
B. FALSE
Answer A
http://shakthydoss.com 24
Data Import/Export
What happen when execute the code.
vec <- c(1,"hello",TRUE)
A. vec is assigned with multiple values.
B. Nothing happens.
C. ERROE
D. vec has only one value and that is TRUE.
Answer C
http://shakthydoss.com 25
Data Import/Export
Which statement is TRUE
A. Matrix is a three-dimensional collection of values that all have the same
type.
B. A factor can be used to represent a categorical variable.
C. Vector is a two-dimensional collection of values that can have multiple
mode (numeric, character, boolean).
D. At maximum a single data frame can hold only 20GB of data.
Answer B
http://shakthydoss.com 26
Data Import/Export
What is most appropriate data structure for the below dataset.
A. Matrix
B. Data frame
C. Array
D. List
Answer B
Name Age Gender
Jhon 24 M
Joe 24 M
Nancy 25 F
http://shakthydoss.com 27
Data Import/Export
Function that is used to create array
A. a(vector, dimensions, dimnames)
B. create(vector, dimensions, dimnames)
C. array(vector, dimensions, dimnames)
D. a(vector,dimensions)
Answer C
http://shakthydoss.com 28

More Related Content

What's hot

Python pandas tutorial
Python pandas tutorialPython pandas tutorial
Python pandas tutorialHarikaReddy115
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...Edureka!
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in RRupak Roy
 
Data warehouse concepts
Data warehouse conceptsData warehouse concepts
Data warehouse conceptsobieefans
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factorskrishna singh
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonLifna C.S
 

What's hot (20)

3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
 
Seaborn visualization.pptx
Seaborn visualization.pptxSeaborn visualization.pptx
Seaborn visualization.pptx
 
Python pandas tutorial
Python pandas tutorialPython pandas tutorial
Python pandas tutorial
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Descriptive Statistics in R.pptx
Descriptive Statistics in R.pptxDescriptive Statistics in R.pptx
Descriptive Statistics in R.pptx
 
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
Основы MATLAB. Лекция 1.
Основы MATLAB. Лекция 1.Основы MATLAB. Лекция 1.
Основы MATLAB. Лекция 1.
 
Python array
Python arrayPython array
Python array
 
Python - Lecture 11
Python - Lecture 11Python - Lecture 11
Python - Lecture 11
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
 
Language R
Language RLanguage R
Language R
 
Data warehouse concepts
Data warehouse conceptsData warehouse concepts
Data warehouse concepts
 
Data visualization with R
Data visualization with RData visualization with R
Data visualization with R
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 

Similar to 3 R Tutorial Data Structure

The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of RWinston Chen
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptxkalai75
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to RAngshuman Saha
 
data frames.pptx
data frames.pptxdata frames.pptx
data frames.pptxRacksaviR
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptxAdrien Melquiond
 
Data handling in r
Data handling in rData handling in r
Data handling in rAbhik Seal
 
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxdataKarthik
 
Practical data science_public
Practical data science_publicPractical data science_public
Practical data science_publicLong Nguyen
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Parth Khare
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Laura Hughes
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processingTim Essam
 
R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementDr. Volkan OBAN
 

Similar to 3 R Tutorial Data Structure (20)

The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of R
 
R language introduction
R language introductionR language introduction
R language introduction
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
R교육1
R교육1R교육1
R교육1
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
R training3
R training3R training3
R training3
 
data frames.pptx
data frames.pptxdata frames.pptx
data frames.pptx
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptx
 
Data handling in r
Data handling in rData handling in r
Data handling in r
 
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
 
R Introduction
R IntroductionR Introduction
R Introduction
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Practical data science_public
Practical data science_publicPractical data science_public
Practical data science_public
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
 
R Basics
R BasicsR Basics
R Basics
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processing
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data Management
 

Recently uploaded

dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Digi Khata Problem along complete plan.pptx
Digi Khata Problem along complete plan.pptxDigi Khata Problem along complete plan.pptx
Digi Khata Problem along complete plan.pptxTanveerAhmed817946
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 

Recently uploaded (20)

dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
Digi Khata Problem along complete plan.pptx
Digi Khata Problem along complete plan.pptxDigi Khata Problem along complete plan.pptx
Digi Khata Problem along complete plan.pptx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 

3 R Tutorial Data Structure

  • 1. R Programming Sakthi Dasan Sekar http://shakthydoss.com 1
  • 2. Data structures a) Vector b) Matrix c) Array d) Data frame e) List http://shakthydoss.com 2
  • 3. Data structure Vectors are one-dimensional arrays a <- c(1, 2, 5, 3, 6, -2, 4) b <- c("one", "two", "three") c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE) a is numeric vector, b is a character vector, and c is a logical vector http://shakthydoss.com 3
  • 4. Data structure Scalars are one-element vectors. f <- 3 g <- "US" h <- TRUE. They’re used to hold constants. http://shakthydoss.com 4
  • 5. Data structure The colon operator : a <- c(1:5) is equivalent to a <- c(1,2, 3, 4, 5) http://shakthydoss.com 5
  • 6. Data structure Vector You can refer to elements of a vector using a numeric vector of positions within brackets. Example vec <- c(“a”, “b”, “c”, “d”, “e”, ”f”) vec[1] # will return the first element in the vector vec[c(2,4)] # will return the 2nd and 4th element in the vector. http://shakthydoss.com 6
  • 7. Data structure Matrices Matrix are two-dimensional data structure in R. Elements in matrix should have same mode (numeric, character, or logical). Matrices are created with the matrix() function. vector <- c(1,2,3,4) foo <- matrix(vector, nrow=2, ncol=2) http://shakthydoss.com 7
  • 8. Data structure Matrices byrow (optional parameter) byrow=TRUE, matrix elements are filled by row wise. byrow=FALSE, matrix elements are filled by column wise. foo <- matrix(vector, nrow=2, ncol=2, byrow = TRUE) foo <- matrix(vector, nrow=2, ncol=2, byrow = FALSE) http://shakthydoss.com 8
  • 9. Data structure Matrix element can be accessed by subscript and brackets Example mat <- matrix(c(1:4), nrow=2,ncol = 2) mat[1,] # returns first row in the matrix. mat[2,] # returns second row in the matrix. mat[,1] # returns first column in the matrix. mat[,2] # returns second column in the matrix. mat[1,2] # return element at first row of second column. http://shakthydoss.com 9
  • 10. Data structure Array Arrays are similar to matrices but can have more than two dimensions Arrays are created with the array() function. array(vector, dimensions, dimnames) a <- matrix(c(1,1,1,1) , 2, 2) b <- matrix(c(2,2,2,2) , 2, 2) foo <- array(c(a,b), c(2,2,2)) http://shakthydoss.com 10
  • 11. Data structure Array array elements can be accessed in the same way a matrices. foo[1,,] # returns all elements in first dimension foo[2,,] # returns all element in second dimension foo[2,1,] # returns only first row element in second dimension http://shakthydoss.com 11
  • 12. Data structure Data frame Data frames are the most commonly used data structure in R. Data frame is more like general matrix but its columns can contain different modes of data (numeric, character, etc.) A data frame is created with the data.frame() function data.frame(col1, col2, col3,..) name <- c( “joe” , “jhon” , “Nancy” ) sex <- c(“M”, “M”, “F”) age <- c(27,26,26) foo <- data.frame(name,sex,age) http://shakthydoss.com 12
  • 13. Data structure Data frame Accessing data frame elements can be straight forward. Element can be accessed by column names. Example foo$name # retruns name vector in the data frame foo$age # retuns age vector in the data frame foo$age[2] # retuns second element of age vector in the data frame http://shakthydoss.com 13
  • 14. Data structure Factors Categorical variables in R are called factors. Status (poor, improved, excellent) and Gender (Male, Female) are good example of an categorical variables. Factor are created using factor() function. gender <- c(“Male", “Female“, “Female”, “Male”) status <- c(“Poor”, “Improved” “Excellent”, “Poor” , “Excellent”) factor_gender <- factor(gender) # factor_genter has two levels called Male and Female factor_status <- factor(status) # factor_status has three levels called Poor, Improved and Excellent. http://shakthydoss.com 14
  • 15. Data structure List Lists are the most complex data structure in R List may contain a combination of vectors, matrices, data frames, and even other lists. You create a list using the list() function vec <- c(1,2,3,4) mat <- matrix(vec,2,2) foo <- list(vec, mat) http://shakthydoss.com 15
  • 16. Data Import/Export Import Excel File Quite frequently, the sample data is in Excel format, and needs to be imported into R prior to use. library(gdata) # load gdata package help(read.xls) # documentation mydata = read.xls("mydata.xls") # read from first sheet http://shakthydoss.com 16
  • 17. Data Import/Export Import Excel File Alternate package XLConnect library(XLConnect) wk = loadWorkbook("mydata.xls") df = readWorksheet(wk, sheet="Sheet1") http://shakthydoss.com 17
  • 18. Data Import/Export Import Minitab File If the data file is in Minitab Portable Worksheet format, it can be opened with the function read.mtp from the foreign package. It returns a list of components in the Minitab worksheet. library(foreign) # load the foreign package help(read.mtp) # documentation mydata = read.mtp("mydata.mtp") # read from .mtp file http://shakthydoss.com 18
  • 19. Data Import/Export Import Table File A data table can resides in a text file. The cells inside the table are separated by blank characters. Here is an example of a table with 4 rows and 3 columns. 100 a1 b1 200 a2 b2 300 a3 b3 400 a4 b4 help(read.table) #documentation mydata = read.table("mydata.txt") http://shakthydoss.com 19
  • 20. Data Import/Export Import CSV File The sample data can also be in comma separated values (CSV) format. Each cell inside such data file is separated by a special character, which usually is a comma. help(read.csv) #documentation mydata = read.csv("mydata.csv", sep=",") http://shakthydoss.com 20
  • 21. Data Import/Export Export Table file help(write.table) #documentation write.table(mydata, "c:/mydata.txt", sep="t") Export Excel file library(xlsx) help(write.xlsx) #documentation write.xlsx(mydata, "c:/mydata.xlsx") http://shakthydoss.com 21
  • 22. Data Import/Export Export CSV file help(write.csv) write.csv(mydate, file = "mydata.csv") Avoid writing the headers write.csv(mydata, file = "mydata.csv", row.names=FALSE) http://shakthydoss.com 22
  • 24. Data Import/Export Every individual data value has a data type that tells us what sort of value it is. A. TRUE B. FALSE Answer A http://shakthydoss.com 24
  • 25. Data Import/Export What happen when execute the code. vec <- c(1,"hello",TRUE) A. vec is assigned with multiple values. B. Nothing happens. C. ERROE D. vec has only one value and that is TRUE. Answer C http://shakthydoss.com 25
  • 26. Data Import/Export Which statement is TRUE A. Matrix is a three-dimensional collection of values that all have the same type. B. A factor can be used to represent a categorical variable. C. Vector is a two-dimensional collection of values that can have multiple mode (numeric, character, boolean). D. At maximum a single data frame can hold only 20GB of data. Answer B http://shakthydoss.com 26
  • 27. Data Import/Export What is most appropriate data structure for the below dataset. A. Matrix B. Data frame C. Array D. List Answer B Name Age Gender Jhon 24 M Joe 24 M Nancy 25 F http://shakthydoss.com 27
  • 28. Data Import/Export Function that is used to create array A. a(vector, dimensions, dimnames) B. create(vector, dimensions, dimnames) C. array(vector, dimensions, dimnames) D. a(vector,dimensions) Answer C http://shakthydoss.com 28