Instructions

Please deliver links to an R Markdown file (in GitHub and rpubs.com) with solutions to the problems below. You may work in a small group, but please submit separately with names of all group participants in your submission.

Normalization Question 1

Provide an example of at least three dataframes in R that demonstrate normalization. The dataframes can contain any data, either real or synthetic. Although normalization is typically done in SQL and relational databases, you are expected to show this example in R, as it is our main work environment in this course.

DataFrame 1:Product ratings

## Product ratings
Product_ratings <- data.frame(
  ProductID = 1:6,
  Rating = c(4.1, 3.7, 5.0, 4.4, 3.2, 4.5),
  Price = c(50, 60, 80, 70, 40, 45)
)

print(Product_ratings)
##   ProductID Rating Price
## 1         1    4.1    50
## 2         2    3.7    60
## 3         3    5.0    80
## 4         4    4.4    70
## 5         5    3.2    40
## 6         6    4.5    45
# Function to normalize each numeric column in a dataframe
normalize_df <- function(df) {
  # Loop through all numeric columns and apply Min-Max normalization
  df_normalized <- df
  for (col in names(df)) {
    if (is.numeric(df[[col]])) {
      min_val <- min(df[[col]], na.rm = TRUE)
      max_val <- max(df[[col]], na.rm = TRUE)
      df_normalized[[col]] <- (df[[col]] - min_val) / (max_val - min_val)
    }
  }
  return(df_normalized)
}

Product_ratings_normalized <- normalize_df(Product_ratings)

print(Product_ratings_normalized)
##   ProductID    Rating Price
## 1       0.0 0.5000000 0.250
## 2       0.2 0.2777778 0.500
## 3       0.4 1.0000000 1.000
## 4       0.6 0.6666667 0.750
## 5       0.8 0.0000000 0.000
## 6       1.0 0.7222222 0.125

DataFrame 2: Sales data with different units

## Sales data with different units
Sales_Data <- data.frame(
  SalesID = 1:5,
  UnitsSold = c(120, 150, 80, 200, 50),
  Revenue = c(1000, 2000, 1500, 2500, 1200)
)

print(Sales_Data)
##   SalesID UnitsSold Revenue
## 1       1       120    1000
## 2       2       150    2000
## 3       3        80    1500
## 4       4       200    2500
## 5       5        50    1200
# Function to normalize each numeric column in a dataframe
normalize_df <- function(df) {
  df_normalized <- df
  for (col in names(df)) {
    if (is.numeric(df[[col]])) {
      min_val <- min(df[[col]], na.rm = TRUE)
      max_val <- max(df[[col]], na.rm = TRUE)
      df_normalized[[col]] <- (df[[col]] - min_val) / (max_val - min_val)
    }
  }
  return(df_normalized)
}

Sales_Data_normalized <- normalize_df(Sales_Data)

print(Sales_Data_normalized)
##   SalesID UnitsSold   Revenue
## 1    0.00 0.4666667 0.0000000
## 2    0.25 0.6666667 0.6666667
## 3    0.50 0.2000000 0.3333333
## 4    0.75 1.0000000 1.0000000
## 5    1.00 0.0000000 0.1333333

DataFrame3: Student Data including Student Test Scores and Study Hours

# DataFrame: Student Test Scores and Study Hours
Student_Data <- data.frame(
  StudentID = 1:6,
  TestScore = c(78, 85, 92, 65, 88, 73),
  StudyHours = c(10, 15, 12, 8, 20, 11)
)

print(Student_Data)
##   StudentID TestScore StudyHours
## 1         1        78         10
## 2         2        85         15
## 3         3        92         12
## 4         4        65          8
## 5         5        88         20
## 6         6        73         11
# Function to normalize each numeric column in a dataframe
normalize_df <- function(df) {
  df_normalized <- df
  for (col in names(df)) {
    if (is.numeric(df[[col]])) {
      min_val <- min(df[[col]], na.rm = TRUE)
      max_val <- max(df[[col]], na.rm = TRUE)
      df_normalized[[col]] <- (df[[col]] - min_val) / (max_val - min_val)
    }
  }
  return(df_normalized)
}

Student_Data_normalized <- normalize_df(Student_Data)

print(Student_Data_normalized)
##   StudentID TestScore StudyHours
## 1       0.0 0.4814815  0.1666667
## 2       0.2 0.7407407  0.5833333
## 3       0.4 1.0000000  0.3333333
## 4       0.6 0.0000000  0.0000000
## 5       0.8 0.8518519  1.0000000
## 6       1.0 0.2962963  0.2500000

Character Manipulation Question 2

Using the 173 majors listed in fivethirtyeight.com’s College Majors dataset [https://fivethirtyeight.com/features/the-economic-guide-to-picking-a-college-major/], provide code that identifies the majors that contain either “DATA” or “STATISTICS”

# Load library
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
# Load the dataset 
url <- "https://raw.githubusercontent.com/Badigun/Data-607-Assignments/refs/heads/main/majors-list.csv"
majors_list <- read.csv(url)

# Check the dataset
head(majors_list)
##   FOD1P                                 Major                  Major_Category
## 1  1100                   GENERAL AGRICULTURE Agriculture & Natural Resources
## 2  1101 AGRICULTURE PRODUCTION AND MANAGEMENT Agriculture & Natural Resources
## 3  1102                AGRICULTURAL ECONOMICS Agriculture & Natural Resources
## 4  1103                       ANIMAL SCIENCES Agriculture & Natural Resources
## 5  1104                          FOOD SCIENCE Agriculture & Natural Resources
## 6  1105            PLANT SCIENCE AND AGRONOMY Agriculture & Natural Resources
# Filter majors that contain "DATA" or "STATISTICS"
filtered_Majors_Data <- majors_list %>%
  filter(grepl("DATA|STATISTICS", Major, ignore.case = TRUE))

# Display the filtered majors
print(filtered_Majors_Data$Major)
## [1] "MANAGEMENT INFORMATION SYSTEMS AND STATISTICS"
## [2] "COMPUTER PROGRAMMING AND DATA PROCESSING"     
## [3] "STATISTICS AND DECISION SCIENCE"

The two exercises below are taken from R for Data Science, 14.3.5.1 in the on-line version:

Question 3: Describe, in words, what these expressions will match:

(.)\1\1 Answer: This expression matches any three consecutive characters where the first character is repeated two more times. For example, it will match strings like “aaa”, “bbb” where the first character repeats twice more. “(.)(.)\2\1” Answer: This expression matches a string with two characters, followed by those same two characters in reverse order. For example, it will match strings like “abba”. The first character is matched again as the last character, and the second character is matched again as the second-to-last character. (..)\1 Answer: This expression matches a string where the first two characters are exactly repeated after them. For example, it will match strings like “abab”, or “1212”. It requires that the first two characters are the same as the next two characters. “(.).\1.\1” Answer: This expression matches strings where the first character appears three times, with two other characters in between. For example, it will match strings like “aXa”. “(.)(.)(.).*\3\2\1” Answer: This expression matches strings where the first three characters are followed by any characters, and then those three characters are repeated in reverse order. For example, it will match strings like “abcxyzcba”, or “123xyzyx321”. The first three characters appear again after some characters, but in reverse order.

Question 4: Construct regular expressions to match words that: