SlideShare a Scribd company logo
1 of 46
Download to read offline
Tabulating
data with
 2012-10-22 @HSPH
Kazuki Yoshida, M.D.
  MPH-CLE student


                       FREEDOM
                       TO	
  KNOW
Group Website is at:
http://rpubs.com/kaz_yos/useR_at_HSPH
Previously in this group
n   Introduction

n   Reading Data into R (1)

n   Reading Data into R (2)

n   Descriptive statistics




                    Group Website: http://rpubs.com/kaz_yos/useR_at_HSPH
Menu

n   Categorical data

n   How to tabulate

n   Get sums and proportions
Ingredients
          Epi/Stat                   Programming
n   Tables                   n   data()

n   Cross tables             n   table(), summary()

n   Stratified tables         n   prop.table()

                              n   addmargins()

                              n   xtabs(), ftable()

                              n   gmodels::CrossTable()

                              n   epiR::epi.2by2()

                              n   Creating categorical variables
country
                                  race
  gender
                                          ethnicity


       Categorical
          data
cancer stage                        education level

               disease severity
Open
R Studio
Install and Load
    vcd epiR
We will use “Arthritis” dataset in vcd package

Load built-in dataset         Named “Arthritis”




 data(Arthritis)
Indexing: extraction of data from
           data frame

Extract 1st to 17th rows      Show all columns



 Arthritis[1:17 ,                           ]
    Colon in between
                           Don’t forget comma
Treatment vector in Arthritis data frame




                        Five vectors of same
                        length tied together
summary of whole
    dataset




  summary
               summary(Arthritis)
Your turn adopted from Hadley Wickham




n   summary(Arthritis)
Accessing a single variable in data set

 dataset name             variable name



Arthritis$Treatment
Arthritis$Treatment




factor levels (categories)
Check factor levels of a vector




              levels
             levels(Arthritis$Treatment)
Your turn    adopted from Hadley Wickham




n   Arthritis$Improved
n   levels(Arthritis$Improved)
This is an ordered factor
factor
factor is categorical
   variable in R
Create a single
variable summary




        table
            table(Arthritis$Improved)
Your turn       adopted from Hadley Wickham




n   table(Arthritis$Improved)
Convert tables to proportions




  prop.table
                  table(table.object)
Your turn      adopted from Hadley Wickham




n   Improved.cat <- table(Arthritis$Improved)
n   prop.table(Improved.cat)
Create cross tables




        xtabs
      xtabs(formula = ~ , data = Arthritis)
Your turn   adopted from Hadley Wickham




n   xtabs(~ Treatment +Improved,                Arthritis)
n   xtabs(~ Treatment +Improved +Sex, Arthritis)
2nd dimention
   1st
dimention




                      3rd dimention
Add margins to tables




 addmargins
            addmargins(table.object)
Your turn     adopted from Hadley Wickham




n   tab1 <- xtabs(~ Treatment +Improved, Arthritis)
n   addmargins(tab1)
Create flat tables
                         Good for ≥ 3 dimentional




      ftable
       ftable(..., exclude = c(NA, NaN),
     row.vars = NULL, col.vars = NULL)
Your turn    adopted from Hadley Wickham




n   tab2 <- xtabs(~ Treatment +Improved +Sex, Arthritis)

n   ftable(tab2)
Proportions again




prop.table
         table(cross.table.object)
Your turn     adopted from Hadley Wickham




n   tab3 <- xtabs(~ Treatment +Improved, Arthritis)
n   prop.table(tab3)      # proportion to total
n   prop.table(tab3, 1) # proportion to row sum
                                                 1st dimension

n   prop.table(tab3, 2) # proportion to2nd dimension sum
                                         column
Chi-squared test




chisq.test
      chisq.test(cross.table.object)
Fisher’s exact test




fisher.test
       fisher.test(cross.table.object)
Your turn     adopted from Hadley Wickham




n   tab3 <- xtabs(~ Treatment +Improved, Arthritis)
n   chisq.test(tab3)
n   fisher.test(tab3)
SAS-like cross tables             available in
                                gmodels package



 CrossTable
                CrossTable(tab.2d)
Your turn     adopted from Hadley Wickham




n   tab3 <- xtabs(~ Treatment +Improved, Arthritis)

n   CrossTable(tab3)
2x2 table with RR RD OR               available in
                                     epiR package




     epi.2x2
                 epi.2x2(tab.2by2)
Your turn    adopted from Hadley Wickham




n   tab.2by2 <- xtabs(~ Sex +Treatment, Arthritis)
n   epi.2by2(tab.2by2, units = 1)
Creating factor
Data in Excel
                factor   factor   Integer
To convert number vector to factor vector




dat$Stage <- factor(dat$Stage)
To convert back to number




dat$Stage <- as.numeric(as.character(dat$Stage))
Categorical data with R

More Related Content

What's hot

Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Laura Hughes
 
Transpose and manipulate character strings
Transpose and manipulate character strings Transpose and manipulate character strings
Transpose and manipulate character strings Rupak Roy
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data framekrishna singh
 
SAS and R Code for Basic Statistics
SAS and R Code for Basic StatisticsSAS and R Code for Basic Statistics
SAS and R Code for Basic StatisticsAvjinder (Avi) Kaler
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Barry DeCicco
 
Is there a perfect data-parallel programming language? (Experiments with More...
Is there a perfect data-parallel programming language? (Experiments with More...Is there a perfect data-parallel programming language? (Experiments with More...
Is there a perfect data-parallel programming language? (Experiments with More...Julian Hyde
 
Greenplum 6 Changes
Greenplum 6 ChangesGreenplum 6 Changes
Greenplum 6 ChangesVMware Tanzu
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
Manipulating Data using base R package
Manipulating Data using base R package Manipulating Data using base R package
Manipulating Data using base R package Rupak Roy
 
5. working on data using R -Cleaning, filtering ,transformation, Sampling
5. working on data using R -Cleaning, filtering ,transformation, Sampling5. working on data using R -Cleaning, filtering ,transformation, Sampling
5. working on data using R -Cleaning, filtering ,transformation, Samplingkrishna singh
 
R short-refcard
R short-refcardR short-refcard
R short-refcardconline
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in REshwar Sai
 
Day 5b statistical functions.pptx
Day 5b   statistical functions.pptxDay 5b   statistical functions.pptx
Day 5b statistical functions.pptxAdrien Melquiond
 

What's hot (20)

R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
 
Transpose and manipulate character strings
Transpose and manipulate character strings Transpose and manipulate character strings
Transpose and manipulate character strings
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
R seminar dplyr package
R seminar dplyr packageR seminar dplyr package
R seminar dplyr package
 
SAS and R Code for Basic Statistics
SAS and R Code for Basic StatisticsSAS and R Code for Basic Statistics
SAS and R Code for Basic Statistics
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
Is there a perfect data-parallel programming language? (Experiments with More...
Is there a perfect data-parallel programming language? (Experiments with More...Is there a perfect data-parallel programming language? (Experiments with More...
Is there a perfect data-parallel programming language? (Experiments with More...
 
Greenplum 6 Changes
Greenplum 6 ChangesGreenplum 6 Changes
Greenplum 6 Changes
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
R gráfico
R gráficoR gráfico
R gráfico
 
Manipulating Data using base R package
Manipulating Data using base R package Manipulating Data using base R package
Manipulating Data using base R package
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Data manipulation language
Data manipulation languageData manipulation language
Data manipulation language
 
5. working on data using R -Cleaning, filtering ,transformation, Sampling
5. working on data using R -Cleaning, filtering ,transformation, Sampling5. working on data using R -Cleaning, filtering ,transformation, Sampling
5. working on data using R -Cleaning, filtering ,transformation, Sampling
 
Day 2b i/o.pptx
Day 2b   i/o.pptxDay 2b   i/o.pptx
Day 2b i/o.pptx
 
R short-refcard
R short-refcardR short-refcard
R short-refcard
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
 
Day 5b statistical functions.pptx
Day 5b   statistical functions.pptxDay 5b   statistical functions.pptx
Day 5b statistical functions.pptx
 
Day 2 repeats.pptx
Day 2 repeats.pptxDay 2 repeats.pptx
Day 2 repeats.pptx
 

Viewers also liked

Viewers also liked (7)

Basic R
Basic RBasic R
Basic R
 
Variables
VariablesVariables
Variables
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
Chi squared test
Chi squared testChi squared test
Chi squared test
 
Variables
 Variables Variables
Variables
 
Types of Variables
Types of VariablesTypes of Variables
Types of Variables
 
Variables And Measurement Scales
Variables And Measurement ScalesVariables And Measurement Scales
Variables And Measurement Scales
 

Similar to Categorical data with R

categorical data analysis in r studioppt.pptx
categorical data analysis in r studioppt.pptxcategorical data analysis in r studioppt.pptx
categorical data analysis in r studioppt.pptxMinilikDerseh1
 
R command cheatsheet.pdf
R command cheatsheet.pdfR command cheatsheet.pdf
R command cheatsheet.pdfNgcnh947953
 
Broom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesBroom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesWork-Bench
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.Dr. Volkan OBAN
 
R Programming Reference Card
R Programming Reference CardR Programming Reference Card
R Programming Reference CardMaurice Dawson
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfTimothy McBush Hiele
 
IR-ranking
IR-rankingIR-ranking
IR-rankingFELIX75
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in Rnaroranisha
 
R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementDr. Volkan OBAN
 
Three way join in one round on hadoop
Three way join in one round on hadoopThree way join in one round on hadoop
Three way join in one round on hadoopIraj Hedayati
 
2d function table
2d function table2d function table
2d function tableD Sanders
 
TENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHONTENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHONAndré Panisson
 

Similar to Categorical data with R (20)

categorical data analysis in r studioppt.pptx
categorical data analysis in r studioppt.pptxcategorical data analysis in r studioppt.pptx
categorical data analysis in r studioppt.pptx
 
20170509 rand db_lesugent
20170509 rand db_lesugent20170509 rand db_lesugent
20170509 rand db_lesugent
 
R command cheatsheet.pdf
R command cheatsheet.pdfR command cheatsheet.pdf
R command cheatsheet.pdf
 
@ R reference
@ R reference@ R reference
@ R reference
 
Broom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesBroom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data Frames
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.
 
Reference card for R
Reference card for RReference card for R
Reference card for R
 
tidyr.pdf
tidyr.pdftidyr.pdf
tidyr.pdf
 
R Programming Reference Card
R Programming Reference CardR Programming Reference Card
R Programming Reference Card
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
IR-ranking
IR-rankingIR-ranking
IR-ranking
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in R
 
R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data Management
 
Three way join in one round on hadoop
Three way join in one round on hadoopThree way join in one round on hadoop
Three way join in one round on hadoop
 
2d function table
2d function table2d function table
2d function table
 
TENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHONTENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHON
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 

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
 
(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
 
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 (18)

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
 
(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
 
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
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Categorical data with R

  • 1. Tabulating data with 2012-10-22 @HSPH Kazuki Yoshida, M.D. MPH-CLE student FREEDOM TO  KNOW
  • 2. Group Website is at: http://rpubs.com/kaz_yos/useR_at_HSPH
  • 3. Previously in this group n Introduction n Reading Data into R (1) n Reading Data into R (2) n Descriptive statistics Group Website: http://rpubs.com/kaz_yos/useR_at_HSPH
  • 4. Menu n Categorical data n How to tabulate n Get sums and proportions
  • 5. Ingredients Epi/Stat Programming n Tables n data() n Cross tables n table(), summary() n Stratified tables n prop.table() n addmargins() n xtabs(), ftable() n gmodels::CrossTable() n epiR::epi.2by2() n Creating categorical variables
  • 6. country race gender ethnicity Categorical data cancer stage education level disease severity
  • 8. Install and Load vcd epiR
  • 9. We will use “Arthritis” dataset in vcd package Load built-in dataset Named “Arthritis” data(Arthritis)
  • 10. Indexing: extraction of data from data frame Extract 1st to 17th rows Show all columns Arthritis[1:17 , ] Colon in between Don’t forget comma
  • 11. Treatment vector in Arthritis data frame Five vectors of same length tied together
  • 12. summary of whole dataset summary summary(Arthritis)
  • 13. Your turn adopted from Hadley Wickham n summary(Arthritis)
  • 14. Accessing a single variable in data set dataset name variable name Arthritis$Treatment
  • 16. Check factor levels of a vector levels levels(Arthritis$Treatment)
  • 17. Your turn adopted from Hadley Wickham n Arthritis$Improved n levels(Arthritis$Improved)
  • 18. This is an ordered factor
  • 20. factor is categorical variable in R
  • 21. Create a single variable summary table table(Arthritis$Improved)
  • 22. Your turn adopted from Hadley Wickham n table(Arthritis$Improved)
  • 23. Convert tables to proportions prop.table table(table.object)
  • 24. Your turn adopted from Hadley Wickham n Improved.cat <- table(Arthritis$Improved) n prop.table(Improved.cat)
  • 25. Create cross tables xtabs xtabs(formula = ~ , data = Arthritis)
  • 26. Your turn adopted from Hadley Wickham n xtabs(~ Treatment +Improved, Arthritis) n xtabs(~ Treatment +Improved +Sex, Arthritis)
  • 27. 2nd dimention 1st dimention 3rd dimention
  • 28. Add margins to tables addmargins addmargins(table.object)
  • 29. Your turn adopted from Hadley Wickham n tab1 <- xtabs(~ Treatment +Improved, Arthritis) n addmargins(tab1)
  • 30. Create flat tables Good for ≥ 3 dimentional ftable ftable(..., exclude = c(NA, NaN), row.vars = NULL, col.vars = NULL)
  • 31. Your turn adopted from Hadley Wickham n tab2 <- xtabs(~ Treatment +Improved +Sex, Arthritis) n ftable(tab2)
  • 32. Proportions again prop.table table(cross.table.object)
  • 33. Your turn adopted from Hadley Wickham n tab3 <- xtabs(~ Treatment +Improved, Arthritis) n prop.table(tab3) # proportion to total n prop.table(tab3, 1) # proportion to row sum 1st dimension n prop.table(tab3, 2) # proportion to2nd dimension sum column
  • 34. Chi-squared test chisq.test chisq.test(cross.table.object)
  • 35. Fisher’s exact test fisher.test fisher.test(cross.table.object)
  • 36. Your turn adopted from Hadley Wickham n tab3 <- xtabs(~ Treatment +Improved, Arthritis) n chisq.test(tab3) n fisher.test(tab3)
  • 37. SAS-like cross tables available in gmodels package CrossTable CrossTable(tab.2d)
  • 38. Your turn adopted from Hadley Wickham n tab3 <- xtabs(~ Treatment +Improved, Arthritis) n CrossTable(tab3)
  • 39.
  • 40. 2x2 table with RR RD OR available in epiR package epi.2x2 epi.2x2(tab.2by2)
  • 41. Your turn adopted from Hadley Wickham n tab.2by2 <- xtabs(~ Sex +Treatment, Arthritis) n epi.2by2(tab.2by2, units = 1)
  • 43. Data in Excel factor factor Integer
  • 44. To convert number vector to factor vector dat$Stage <- factor(dat$Stage)
  • 45. To convert back to number dat$Stage <- as.numeric(as.character(dat$Stage))

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n