-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson18-tables-plyr.R
47 lines (27 loc) · 964 Bytes
/
lesson18-tables-plyr.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
demo <- read.csv("demographics.csv")
View(demo)
##########
### how to create frequency tables
### with the package plyr (function count)
##########
### we will build a table for the variable educ (education level)
### this table will contain the following:
### absolute frequencies (counts), cumulative counts,
### relative frequencies and cumulative relative frequencies
### load the package
require(plyr)
### build the initial table, with the absolute frequencies
mytable <- count(demo, 'educ')
print(mytable)
# compute the percentages (relative frequencies)
perc <- mytable$freq/nrow(demo)
print(perc)
### compute the cumulative counts
cumul <- cumsum(mytable$freq)
print(cumul)
### compute the cumulative percentages
cumperc <- cumul/nrow(demo)
print(cumperc)
# add the cumulative counts and the percentages to the iniatial table
mytable <- cbind(mytable, cumul, perc, cumperc)
print(mytable)