# Load 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
library(tibble)
library(formattable)
# Auto-install missing packages if needed
packages <- c("dplyr", "tibble", "formattable")
for(p in packages){
if(!require(p, character.only = TRUE)){
install.packages(p, dependencies = TRUE)
library(p, character.only = TRUE)
}
}
# Use built-in mtcars dataset
df <- as_tibble(mtcars)
# Preview data
head(df)
# Print dataset dimensions
cat("The dataset has", nrow(df), "rows and", ncol(df), "columns.\n")
## The dataset has 32 rows and 11 columns.
# Summary statistics for mpg
mean_mpg <- mean(df$mpg)
median_mpg <- median(df$mpg)
range_mpg <- range(df$mpg)
sd_mpg <- sd(df$mpg)
var_mpg <- var(df$mpg)
cat("For miles per gallon (mpg):\n",
"Mean =", round(mean_mpg, 2), "\n",
"Median =", round(median_mpg, 2), "\n",
"Range =", round(range_mpg[1], 2), "to", round(range_mpg[2], 2), "\n",
"Standard Deviation =", round(sd_mpg, 2), "\n",
"Variance =", round(var_mpg, 2), "\n")
## For miles per gallon (mpg):
## Mean = 20.09
## Median = 19.2
## Range = 10.4 to 33.9
## Standard Deviation = 6.03
## Variance = 36.32
# Percentiles
percentiles <- quantile(df$mpg, probs = c(0.25, 0.75))
cat("25th percentile of mpg =", round(percentiles[1], 2),
"and 75th percentile =", round(percentiles[2], 2), "\n")
## 25th percentile of mpg = 15.43 and 75th percentile = 22.8
# Correlation between mpg and hp
cor_mpg_hp <- cor(df$mpg, df$hp)
cat("Correlation between mpg and horsepower (hp) =", round(cor_mpg_hp, 2), "\n")
## Correlation between mpg and horsepower (hp) = -0.78
# Subset cars with mpg > 25
high_mpg <- df %>% filter(mpg > 25)
cat("Number of cars with mpg > 25:", nrow(high_mpg), "\n")
## Number of cars with mpg > 25: 6
# Mean and SD of hp for subset
mean_hp_high <- mean(high_mpg$hp)
sd_hp_high <- sd(high_mpg$hp)
cat("For cars with mpg > 25:\n",
"Mean horsepower =", round(mean_hp_high, 2), "\n",
"SD of horsepower =", round(sd_hp_high, 2), "\n")
## For cars with mpg > 25:
## Mean horsepower = 75.5
## SD of horsepower = 22.31
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.