forked from ahulman/Assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch1_analysis.R
49 lines (28 loc) · 1.35 KB
/
ch1_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
##set Wd
#source ch1_simContinuous and ch1_simCategorical
source('ch1_simContinuous.R')
source('ch1_simCategorical.R')
# merge two data frames by ID,
total <- merge(dataContinuous,dataCategorical, by=c("id","sex"))
#change numeric storage type to factor
total$ethnic <- factor (total$ethnic,
levels = c(0, 1),
labels = c("non-white", "white"))
total$sex <- factor (total$sex,
levels = c(0, 1),
labels = c("male", "female"))
#make summary of continuous variables use tapply summary Calculate descriptive statistics overall (e.g. summary function) and by sex (summary within a tapply) using the summary/tapply and table/prop.table functions for continuous and categorical variables, respectively.
#* Median (Q1-Q3) for continuous variables
#* Frequency (%) for categorical variables
summary(total)
#Descriptive statistics stratified by sex
tapply(total$age, total$sex, summary)
tapply(total$bmi, total$sex, summary)
prop.table(table(total$sex, total$ethnic), margin = 1)
prop.table(table(total$sex, total$smoke), margin = 1)
tapply(total$age, total$sex, hist)
tapply(total$age, total$sex, summary)
tapply(total$bmi, total$sex, summary)
prop.table(table(total$sex, total$ethnic), margin = 1)
prop.table(table(total$sex, total$smoke), margin = 1)
tapply(total$age, total$sex, hist)