SlideShare a Scribd company logo
1 of 20
Download to read offline
Manipulationg
   data in
    2013-02-22 @HSPH
   Kazuki Yoshida, M.D.
     MPH-CLE student

                          FREEDOM
                          TO	
  KNOW
Manipulating data in R

n   What are Objects?
n   What is Class attribute?
n   Various data objects you will see in R.
Objects

n   Just about everything named in R is an object
n   An object is a container that
     n   knows its class (label for what’s inside).
     n   has contents (eg, Actual numbers).
Examples of objects
n   dataset, which you use for analysis (various
     classes)
n   functions, which perform analysis (function class)
n   results, which come out of analysis (various
     classes)
     n   In effect, you always get a new dataset filled
          with results when you analyze data.
Classes of data values
      inside data objects
n   Numeric: Continuous variables
n   Factor: Categorical variables
n   Logical: TRUE/FALSE binary variables
n   etc...
Class?

n   An object’s class tells R how the object should be
     handled.
n   For example, summarizing data should work
     differently for numbers and categories!
Object




                                  iables
                              var
                          ical !
                       gor ide
                  C ate ins

                                Class attribute

    http://en.wikipedia.org/wiki/File:3_D-Box.jpg
Data objects

n   Vector (contains single class of data values)


n   List (contains multiple classes of data values)
Data objects

n   Vector (contains single class of data values)
     n   Array including Matrix
n   List (contains multiple classes of data values)
     n   Data frame
Vector
n   Smallest building block of data objects
n   Single dimension
n   Combination of values of same class
n   vec1 <- c(2013, 2, 15, -10) # combine
n   vec2 <- 1:16 # integers 1 to 16
Vector




1-dimensional
Array/Matrix
n   Vector folded into a multidimensional structure
n   2-dimensional array is a matrix
n   vec3 <- 1:16
n   dim(vec3) <- c(4, 4) # 4 x 4 structure
n   dim(vec3) <- c(2, 2, 4) # 2 x 2 x 4 structure
n   arr1 <- array(1:60, dim = c(3,4,5))
Matrix




Folded vector with dimension
List
n   Combination of any values or objects
n   Can contain objects of multiple classes
n   eg, a list of two vectors, a matrix, three arrays
n   List_name$Variable_name operation with $ operator
n   list1 <- list(first = 1:17, second = matrix(letters, 13,2))
n   list2 <- list(alpha = c(1,4,5,7), beta = c("h","s","p","h"))
List
           Multi-part object




  Can contain vectors,
    arrays, or lists!
Data frame
n   Special case of a list
n   List of same-length vectors vertically aligned
n   df1 <- data.frame(list2)
n   list3 <- list(small = letters, large = LETTERS,
     number = 1:26)
n   df2 <- data.frame(list3)
Data Frame




Multiple vectors of same length tied together!
Access by indexes
n   letters[3] # 1-dimensional object
n   arr1[1,2,3] # 3-dimensional object
n   arr1[1, ,3] # implies 1,(all),3
n   df1[ ,3] # implies (all),3
n   list1[[1]] # list needs [[ ]]
Access named elements
n   list3
n   list3$small
n   list3[["small"]]
n   df1$large
n   df1[, "large"]
20130222 Data structures and manipulation in R

More Related Content

What's hot

Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021Sreedhar Chowdam
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureRai University
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation Anil Kumar Prajapati
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
2nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 12nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 1Aahwini Esware gowda
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithmsVinayKumarV16
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 
Introduction of Data Structure
Introduction of Data StructureIntroduction of Data Structure
Introduction of Data StructureMandavi Classes
 
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation:  Data Files, and Data Cleaning & PreparationAaa ped-6-Data manipulation:  Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & PreparationAminaRepo
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure NUPOORAWSARMOL
 
Elementary data structure
Elementary data structureElementary data structure
Elementary data structureBiswajit Mandal
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 

What's hot (20)

Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
2nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 12nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 1
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithms
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Data structure
Data structureData structure
Data structure
 
Data structures
Data structuresData structures
Data structures
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Introduction of Data Structure
Introduction of Data StructureIntroduction of Data Structure
Introduction of Data Structure
 
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation:  Data Files, and Data Cleaning & PreparationAaa ped-6-Data manipulation:  Data Files, and Data Cleaning & Preparation
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
R training2
R training2R training2
R training2
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Elementary data structure
Elementary data structureElementary data structure
Elementary data structure
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Data structure and its types.
Data structure and its types.Data structure and its types.
Data structure and its types.
 

Similar to 20130222 Data structures and manipulation in R

Similar to 20130222 Data structures and manipulation in R (20)

R data types
R data typesR data types
R data types
 
R교육1
R교육1R교육1
R교육1
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in R
 
8074.pdf
8074.pdf8074.pdf
8074.pdf
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Abstract Algebra and Category Theory
Abstract Algebra and Category Theory
 
Array.ppt
Array.pptArray.ppt
Array.ppt
 
array.ppt
array.pptarray.ppt
array.ppt
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Array ppt
Array pptArray ppt
Array ppt
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
IR-ranking
IR-rankingIR-ranking
IR-ranking
 
M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
DSA Unit II array.pptx
DSA Unit II array.pptxDSA Unit II array.pptx
DSA Unit II array.pptx
 
arrayppt.pptx
arrayppt.pptxarrayppt.pptx
arrayppt.pptx
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
R Programming Reference Card
R Programming Reference CardR Programming Reference Card
R Programming Reference Card
 

More from Kazuki Yoshida

Graphical explanation of causal mediation analysis
Graphical explanation of causal mediation analysisGraphical explanation of causal mediation analysis
Graphical explanation of causal mediation analysisKazuki Yoshida
 
Pharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCT
Pharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCTPharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCT
Pharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCTKazuki Yoshida
 
What is the Expectation Maximization (EM) Algorithm?
What is the Expectation Maximization (EM) Algorithm?What is the Expectation Maximization (EM) Algorithm?
What is the Expectation Maximization (EM) Algorithm?Kazuki Yoshida
 
Propensity Score Methods for Comparative Effectiveness Research with Multiple...
Propensity Score Methods for Comparative Effectiveness Research with Multiple...Propensity Score Methods for Comparative Effectiveness Research with Multiple...
Propensity Score Methods for Comparative Effectiveness Research with Multiple...Kazuki Yoshida
 
Visual Explanation of Ridge Regression and LASSO
Visual Explanation of Ridge Regression and LASSOVisual Explanation of Ridge Regression and LASSO
Visual Explanation of Ridge Regression and LASSOKazuki Yoshida
 
ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...
ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...
ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...Kazuki Yoshida
 
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...Kazuki Yoshida
 
Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...
Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...
Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...Kazuki Yoshida
 
Spacemacs: emacs user's first impression
Spacemacs: emacs user's first impressionSpacemacs: emacs user's first impression
Spacemacs: emacs user's first impressionKazuki Yoshida
 
Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...
Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...
Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...Kazuki Yoshida
 
Multiple Imputation: Joint and Conditional Modeling of Missing Data
Multiple Imputation: Joint and Conditional Modeling of Missing DataMultiple Imputation: Joint and Conditional Modeling of Missing Data
Multiple Imputation: Joint and Conditional Modeling of Missing DataKazuki Yoshida
 
Linear regression with R 2
Linear regression with R 2Linear regression with R 2
Linear regression with R 2Kazuki Yoshida
 
Linear regression with R 1
Linear regression with R 1Linear regression with R 1
Linear regression with R 1Kazuki Yoshida
 
(Very) Basic graphing with R
(Very) Basic graphing with R(Very) Basic graphing with R
(Very) Basic graphing with RKazuki Yoshida
 
Introduction to Deducer
Introduction to DeducerIntroduction to Deducer
Introduction to DeducerKazuki Yoshida
 
Groupwise comparison of continuous data
Groupwise comparison of continuous dataGroupwise comparison of continuous data
Groupwise comparison of continuous dataKazuki Yoshida
 
Categorical data with R
Categorical data with RCategorical data with R
Categorical data with RKazuki Yoshida
 
Install and Configure R and RStudio
Install and Configure R and RStudioInstall and Configure R and RStudio
Install and Configure R and RStudioKazuki Yoshida
 
Reading Data into R REVISED
Reading Data into R REVISEDReading Data into R REVISED
Reading Data into R REVISEDKazuki Yoshida
 

More from Kazuki Yoshida (20)

Graphical explanation of causal mediation analysis
Graphical explanation of causal mediation analysisGraphical explanation of causal mediation analysis
Graphical explanation of causal mediation analysis
 
Pharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCT
Pharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCTPharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCT
Pharmacoepidemiology Lecture: Designing Observational CER to Emulate an RCT
 
What is the Expectation Maximization (EM) Algorithm?
What is the Expectation Maximization (EM) Algorithm?What is the Expectation Maximization (EM) Algorithm?
What is the Expectation Maximization (EM) Algorithm?
 
Propensity Score Methods for Comparative Effectiveness Research with Multiple...
Propensity Score Methods for Comparative Effectiveness Research with Multiple...Propensity Score Methods for Comparative Effectiveness Research with Multiple...
Propensity Score Methods for Comparative Effectiveness Research with Multiple...
 
Emacs Key Bindings
Emacs Key BindingsEmacs Key Bindings
Emacs Key Bindings
 
Visual Explanation of Ridge Regression and LASSO
Visual Explanation of Ridge Regression and LASSOVisual Explanation of Ridge Regression and LASSO
Visual Explanation of Ridge Regression and LASSO
 
ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...
ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...
ENAR 2018 Matching Weights to Simultaneously Compare Three Treatment Groups: ...
 
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
 
Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...
Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...
Comparison of Privacy-Protecting Analytic and Data-sharing Methods: a Simulat...
 
Spacemacs: emacs user's first impression
Spacemacs: emacs user's first impressionSpacemacs: emacs user's first impression
Spacemacs: emacs user's first impression
 
Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...
Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...
Matching Weights to Simultaneously Compare Three Treatment Groups: a Simulati...
 
Multiple Imputation: Joint and Conditional Modeling of Missing Data
Multiple Imputation: Joint and Conditional Modeling of Missing DataMultiple Imputation: Joint and Conditional Modeling of Missing Data
Multiple Imputation: Joint and Conditional Modeling of Missing Data
 
Linear regression with R 2
Linear regression with R 2Linear regression with R 2
Linear regression with R 2
 
Linear regression with R 1
Linear regression with R 1Linear regression with R 1
Linear regression with R 1
 
(Very) Basic graphing with R
(Very) Basic graphing with R(Very) Basic graphing with R
(Very) Basic graphing with R
 
Introduction to Deducer
Introduction to DeducerIntroduction to Deducer
Introduction to Deducer
 
Groupwise comparison of continuous data
Groupwise comparison of continuous dataGroupwise comparison of continuous data
Groupwise comparison of continuous data
 
Categorical data with R
Categorical data with RCategorical data with R
Categorical data with R
 
Install and Configure R and RStudio
Install and Configure R and RStudioInstall and Configure R and RStudio
Install and Configure R and RStudio
 
Reading Data into R REVISED
Reading Data into R REVISEDReading Data into R REVISED
Reading Data into R REVISED
 

20130222 Data structures and manipulation in R

  • 1. Manipulationg data in 2013-02-22 @HSPH Kazuki Yoshida, M.D. MPH-CLE student FREEDOM TO  KNOW
  • 2. Manipulating data in R n What are Objects? n What is Class attribute? n Various data objects you will see in R.
  • 3. Objects n Just about everything named in R is an object n An object is a container that n knows its class (label for what’s inside). n has contents (eg, Actual numbers).
  • 4. Examples of objects n dataset, which you use for analysis (various classes) n functions, which perform analysis (function class) n results, which come out of analysis (various classes) n In effect, you always get a new dataset filled with results when you analyze data.
  • 5. Classes of data values inside data objects n Numeric: Continuous variables n Factor: Categorical variables n Logical: TRUE/FALSE binary variables n etc...
  • 6. Class? n An object’s class tells R how the object should be handled. n For example, summarizing data should work differently for numbers and categories!
  • 7. Object iables var ical ! gor ide C ate ins Class attribute http://en.wikipedia.org/wiki/File:3_D-Box.jpg
  • 8. Data objects n Vector (contains single class of data values) n List (contains multiple classes of data values)
  • 9. Data objects n Vector (contains single class of data values) n Array including Matrix n List (contains multiple classes of data values) n Data frame
  • 10. Vector n Smallest building block of data objects n Single dimension n Combination of values of same class n vec1 <- c(2013, 2, 15, -10) # combine n vec2 <- 1:16 # integers 1 to 16
  • 12. Array/Matrix n Vector folded into a multidimensional structure n 2-dimensional array is a matrix n vec3 <- 1:16 n dim(vec3) <- c(4, 4) # 4 x 4 structure n dim(vec3) <- c(2, 2, 4) # 2 x 2 x 4 structure n arr1 <- array(1:60, dim = c(3,4,5))
  • 14. List n Combination of any values or objects n Can contain objects of multiple classes n eg, a list of two vectors, a matrix, three arrays n List_name$Variable_name operation with $ operator n list1 <- list(first = 1:17, second = matrix(letters, 13,2)) n list2 <- list(alpha = c(1,4,5,7), beta = c("h","s","p","h"))
  • 15. List Multi-part object Can contain vectors, arrays, or lists!
  • 16. Data frame n Special case of a list n List of same-length vectors vertically aligned n df1 <- data.frame(list2) n list3 <- list(small = letters, large = LETTERS, number = 1:26) n df2 <- data.frame(list3)
  • 17. Data Frame Multiple vectors of same length tied together!
  • 18. Access by indexes n letters[3] # 1-dimensional object n arr1[1,2,3] # 3-dimensional object n arr1[1, ,3] # implies 1,(all),3 n df1[ ,3] # implies (all),3 n list1[[1]] # list needs [[ ]]
  • 19. Access named elements n list3 n list3$small n list3[["small"]] n df1$large n df1[, "large"]