-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson17-tables-base-r.R
49 lines (29 loc) · 1.11 KB
/
lesson17-tables-base-r.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
49
demo <- read.csv("demographics.csv")
View(demo)
##########
### how to create frequency tables
### in base R
##########
### we will build a table for the variable educ (education level)
### this table will contain the following:
### absolute frequencies (counts), cumulative absolute frequencies,
### relative frequencies and cumulatitve relative frequencies
### create the initial table (with the counts only)
mytable <- table(demo$educ, exclude = NULL) ### the missing values will be excluded
print(mytable)
### compute the cumulative counts (using the cumsum fuction)
cumul <- cumsum(mytable)
print(cumul)
### compute the relative frequencies
relative <- prop.table(mytable)
print(relative)
### compute the cumulative relative frequencies
n <- nrow(demo) ### number of rows (cases) of the data frame demo
cumulfreq <- cumul/n
print(cumulfreq)
### create the final table with the cbind function
mytable2 <- cbind(Freq=mytable, Cumul=cumul, Relative=relative, CumFreq=cumulfreq)
print(mytable2)
###
### the commands above can be used with numeric variables as well
###