Home » Uncategorized » Frequency Table Using R Part2

Frequency Table Using R Part2

How Do We Construct Frequency Distribution Tables Using R?

Part 2 (Relative Frequeancy Distribution Table, Cumulative Frequency Distribution, and Cumulative Relative Frequency Distribution Table)

We will use States Visited, (StatesV) variable included in first day class survey, Sur1.
  1. Upload Sur1.csv data set from your desktop to R
Sur1 <-read.csv("~/Desktop/Sur1.csv")

NOTE: If case you did not save Sur1.csv file correctly to your desktop, execute the following code
Sur1 <-read.csv(read.csv(file.choose(),header = TRUE))

  1. Examinne the strucure of Sur1 data set
str(Sur1)
## 'data.frame':    20 obs. of  6 variables:
##  $ SEX      : Factor w/ 2 levels "Female","Male": 1 1 1 1 2 2 2 1 1 1 ...
##  $ POLITICS : Factor w/ 3 levels "Conservative",..: 3 3 3 3 3 3 2 2 1 3 ...
##  $ NSiblings: int  4 4 2 2 2 4 5 4 4 1 ...
##  $ StatesV  : int  5 11 6 3 7 3 4 4 6 3 ...
##  $ ShoeS    : num  6 9 8 7.5 13 10.5 9 8 6 8.5 ...
##  $ UsedExcel: int  2 2 2 1 1 3 3 2 2 1 ...
  1. View first six row of the data in spreadsheet format
head(Sur1, 6)
##      SEX POLITICS NSiblings StatesV ShoeS UsedExcel
## 1 Female Moderate         4       5   6.0         2
## 2 Female Moderate         4      11   9.0         2
## 3 Female Moderate         2       6   8.0         2
## 4 Female Moderate         2       3   7.5         1
## 5   Male Moderate         2       7  13.0         1
## 6   Male Moderate         4       3  10.5         3
  1. Attach Sur1 to R in order to work with data variables without using $ symbol (remember to detach(Sur1) after completion of analysis)
attach(Sur1)
  1. Summarize StatesV variable
summary(StatesV)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3.00    4.00    6.00    7.15    9.50   18.00
  1. Construct a frequency distribution table of States Visited using breaks to list class limits
breaks <-seq(0,18, by=3)
  1. Group StatesV variable data into bins
StatesV.cut <-cut(StatesV,breaks)
  1. Construct frequency table for StatesV variable in horizontal display
 table(StatesV.cut)
## StatesV.cut
##   (0,3]   (3,6]   (6,9]  (9,12] (12,15] (15,18] 
##       3       8       4       3       1       1
  1. Transform this table to 2-column display
transform(table(StatesV.cut))
##   StatesV.cut Freq
## 1       (0,3]    3
## 2       (3,6]    8
## 3       (6,9]    4
## 4      (9,12]    3
## 5     (12,15]    1
## 6     (15,18]    1
  1. Convert frequency table to relative frequency table by dividing frequencies by total number of StatesV data, in our case by 20
transform(table(StatesV.cut)/20)
##   StatesV.cut Freq
## 1       (0,3] 0.15
## 2       (3,6] 0.40
## 3       (6,9] 0.20
## 4      (9,12] 0.15
## 5     (12,15] 0.05
## 6     (15,18] 0.05
  1. Convert frequency table to cumulative frequency table by summing frequencies of StatesV data cumulatively
transform(table(StatesV.cut),Cum_Freq=cumsum(Freq))
##   StatesV.cut Freq Cum_Freq
## 1       (0,3]    3        3
## 2       (3,6]    8       11
## 3       (6,9]    4       15
## 4      (9,12]    3       18
## 5     (12,15]    1       19
## 6     (15,18]    1       20
  1. Convert frequency table to cumulative relative frequency table by dividing cumulative frequencies by total number of StatesV data, in our case by 20
transform(table(StatesV.cut),RelCum_Freq=cumsum(Freq)/20)
##   StatesV.cut Freq RelCum_Freq
## 1       (0,3]    3        0.15
## 2       (3,6]    8        0.55
## 3       (6,9]    4        0.75
## 4      (9,12]    3        0.90
## 5     (12,15]    1        0.95
## 6     (15,18]    1        1.00

Leave a comment

Your email address will not be published. Required fields are marked *