Introduction

This week’s focus was on running frequency distributions on a chosen dataset. In this blog post, I’ll walk you through the steps I took to run my first program, display frequency distributions for selected variables, and provide insights into the values and patterns observed.

Step 1: Running the First Program

# Load necessary libraries
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Generate more sample data
set.seed(123)  # For reproducibility
my_dataset <- data.frame(
  variable1 = sample(1:5, 200, replace = TRUE),
  variable2 = sample(c("A", "B", "C"), 200, replace = TRUE),
  variable3 = rnorm(200, mean = 10, sd = 2)
)

# Your program goes here

# Correct any errors highlighted in the program
# ...

Step 2: Frequency Distributions

# Selecting variables and running frequency distributions
selected_data <- my_dataset %>%
  select(variable1, variable2, variable3)

# Displaying frequency tables
table_var1 <- table(selected_data$variable1)
table_var2 <- table(selected_data$variable2)
table_var3 <- table(selected_data$variable3)

# Printing the frequency tables
table_var1
## 
##  1  2  3  4  5 
## 46 40 37 34 43
table_var2
## 
##  A  B  C 
## 67 66 67
table_var3
## 
## 4.67815440306864 4.71370209594204 5.06820361247995 5.55002460702517 
##                1                1                1                1 
## 5.68470732996945 5.89532603219609 5.89555435913254 5.97157900415856 
##                1                1                1                1 
## 6.01450302262283 6.41943752547188 6.49352528171546 6.54653920177134 
##                1                1                1                1 
## 6.69790220862363 6.69890691314978 6.72724146388195 7.12298671017095 
##                1                1                1                1 
## 7.27192509583525 7.28384188125877 7.32245142553005 7.35409777448957 
##                1                1                1                1 
## 7.47863424236245 7.48270274387966 7.51791100638302 7.62313192970404 
##                1                1                1                1 
## 7.66269715115129 7.72882305925131 7.81442558179699 7.82001625517752 
##                1                1                1                1 
## 7.84515869377604 7.88996591479464 7.89497344132253 7.98872748097833 
##                1                1                1                1 
## 8.00643851558496 8.05198083439182 8.10918233775221 8.12292259278621 
##                1                1                1                1 
## 8.21358485989009 8.25385799916507 8.34104477675096 8.45171014078919 
##                1                1                1                1 
## 8.49462206356452 8.55679111912797 8.56306755736148 8.56951562555441 
##                1                1                1                1 
## 8.70590873736346 8.70977208628042 8.73050347018218 8.79698658398644 
##                1                1                1                1 
##    8.82103792297 8.85121089563123 8.85287945846343 8.85629988420887 
##                1                1                1                1 
##  8.8894432973735 8.92238168004481 8.95417524737316 9.03002480687535 
##                1                1                1                1 
## 9.07202551406797 9.07792332223129 9.09932275215058  9.1256809336392 
##                1                1                1                1 
## 9.12870906061619 9.14544142558914 9.17264697281576 9.21863004272204 
##                1                1                1                1 
## 9.23808652141197 9.29091519385204 9.29590708683477 9.30049152157746 
##                1                1                1                1 
## 9.30305510208878  9.3477122149893 9.40968439823285 9.43703576992207 
##                1                1                1                1 
## 9.52129686572377 9.54463187020894 9.57191752002923 9.58041365754298 
##                1                1                1                1 
##  9.5894014850636 9.59843796882176 9.60022034379713  9.6189663959142 
##                1                1                1                1 
## 9.64018753790492 9.75458267804524  9.7884316647005 9.78865733199245 
##                1                1                1                1 
## 9.79623348856956 9.81411796304106 9.85288796172704 9.87835609067985 
##                1                1                1                1 
##  9.8960361876382 9.93392768144802 9.94231689414337 9.97138517366617 
##                1                1                1                1 
## 10.0145801806338 10.0222583744418 10.0385185492507 10.0409014170501 
##                1                1                1                1 
## 10.0691021342677 10.1120334665499 10.1303078651586 10.1986551881757 
##                1                1                1                1 
## 10.2332745671654 10.2426367549816 10.2526317169178 10.2889514094214 
##                1                1                1                1 
## 10.3033610090186 10.3296817358369 10.3306420424841 10.3494527939637 
##                1                1                1                1 
## 10.3636943852397 10.3804606313849 10.4239608667446 10.4545838433459 
##                1                1                1                1 
## 10.4612336612624 10.4748605449821 10.5356700306634 10.5475329820731 
##                1                1                1                1 
## 10.5728488392576 10.6000770904116 10.6281153270125  10.662358345918 
##                1                1                1                1 
## 10.6678058849985 10.6922072391072 10.7282293747101 10.7460093116098 
##                1                1                1                1 
## 10.7547759460479  10.756335544417 10.7568478072737 10.8228598412315 
##                1                1                1                1 
##   10.86056939272 10.8705778978154 10.8776374014289 10.9095385379653 
##                1                1                1                1 
## 10.9124728063596 10.9522665566053 10.9844571401289 11.0202650937573 
##                1                1                1                1 
## 11.0370075317765 11.0707976817363 11.2483749440413 11.2659214260629 
##                1                1                1                1 
## 11.3023865631753 11.3065153589428 11.3198052762852 11.3216405955796 
##                1                1                1                1 
## 11.3413919374679 11.3674910437014 11.4070478055138 11.4254066504886 
##                1                1                1                1 
## 11.4256846400622 11.4303568142221 11.4818000225485  11.512812877091 
##                1                1                1                1 
## 11.5135495275919 11.5577200595671 11.6353188927482 11.6507597255185 
##                1                1                1                1 
## 11.6914630803786 11.7138460217986  11.731558808669 11.7439299081575 
##                1                1                1                1 
## 11.7666056398974 11.7685016400564 11.8227825835919 11.8295465417113 
##                1                1                1                1 
##  11.834349835282 11.9180107555757 12.0298863454873 12.0351172739042 
##                1                1                1                1 
## 12.0493464696367 12.0932576942231 12.1695501797213 12.2205541932776 
##                1                1                1                1 
## 12.2500054916563 12.4200210208805 12.4362172206516 12.4713869246003 
##                1                1                1                1 
## 12.4733500928331 12.4998291419384  12.544533558937 12.5881678122912 
##                1                1                1                1 
## 12.6023519844012  12.656429392074 12.6710352301188 12.7571402739185 
##                1                1                1                1 
## 12.8081005354283 12.8608046823764 13.0078012180091 13.0384354227764 
##                1                1                1                1 
## 13.2537624283894 13.2671368428166 13.3421096577259 13.3688714161882 
##                1                1                1                1 
## 13.4246099546386 13.4485244783129 13.5590058195503 13.9508381080382 
##                1                1                1                1 
## 14.0751480364809 14.7949049600995 14.8335467075764 15.1429162917333 
##                1                1                1                1

Interpretation of Frequency Distributions Now, let’s delve into the insights gained from the frequency distributions:

Variable 1 Values: {value1, value2, value3, …} Frequency: {frequency1, frequency2, frequency3, …} Presence of missing data: Yes/No Variable 2 Values: {value1, value2, value3, …} Frequency: {frequency1, frequency2, frequency3, …} Presence of missing data: Yes/No Variable 3 Values: {value1, value2, value3, …} Frequency: {frequency1, frequency2, frequency3, …} Presence of missing data: Yes/No In the summary above, we can observe the values each variable takes, how often they occur, and whether there’s any missing data. This information lays the groundwork for further analysis in upcoming weeks.

Data Visualization Bar Charts

# Bar chart for Variable 1
barplot(table_var1, main = "Variable 1 Distribution", xlab = "Values", ylab = "Frequency", col = "skyblue")

# Bar chart for Variable 2
barplot(table_var2, main = "Variable 2 Distribution", xlab = "Values", ylab = "Frequency", col = "lightgreen")

Histogram

# Histogram for Variable 3
hist(selected_data$variable3, main = "Variable 3 Distribution", xlab = "Values", col = "pink", border = "black")

Trend Line

# Scatter plot with trend line for Variable 1 and Variable 3
plot(selected_data$variable1, selected_data$variable3, main = "Scatter Plot with Trend Line", 
     xlab = "Variable 1", ylab = "Variable 3", col = "darkblue")
abline(lm(selected_data$variable3 ~ selected_data$variable1), col = "red")