More Related Content Similar to Categorical data with R Similar to Categorical data with R (20) More from Kazuki Yoshida (18) Categorical data with R3. 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
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
14. Accessing a single variable in data set
dataset name variable name
Arthritis$Treatment
17. Your turn adopted from Hadley Wickham
n Arthritis$Improved
n levels(Arthritis$Improved)
22. Your turn adopted from Hadley Wickham
n table(Arthritis$Improved)
24. Your turn adopted from Hadley Wickham
n Improved.cat <- table(Arthritis$Improved)
n prop.table(Improved.cat)
26. Your turn adopted from Hadley Wickham
n xtabs(~ Treatment +Improved, Arthritis)
n xtabs(~ Treatment +Improved +Sex, Arthritis)
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)
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
36. Your turn adopted from Hadley Wickham
n tab3 <- xtabs(~ Treatment +Improved, Arthritis)
n chisq.test(tab3)
n fisher.test(tab3)
38. Your turn adopted from Hadley Wickham
n tab3 <- xtabs(~ Treatment +Improved, Arthritis)
n CrossTable(tab3)
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)
45. To convert back to number
dat$Stage <- as.numeric(as.character(dat$Stage))
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n