---
title: "Assignment R file"
author: "Vishal Bapodara"
date: "2026-02-25"
output: word_document
---
############################################################
# Assignment: Data Manipulation Simulation
############################################################

# -----------------------------
# TASK A: Generate Production Data
# -----------------------------

set.seed(123)  # for reproducibility

BaseOutput <- 50

# Total rows = 30 sols × 6 stations × 4 teams
total_rows <- 30 * 6 * 4

# Pre-allocate vectors
Sol_vec <- numeric(total_rows)
Station_vec <- numeric(total_rows)
Team_vec <- numeric(total_rows)

Output_vec <- numeric(total_rows)

row_index <- 1

# Nested loops
for (sol in 1:30) {
  for (station in 1:6) {
    for (team in 1:4) {
      
      RandomNoise <- sample(-5:5, 1)
      
      Output_value <- BaseOutput + 
        (station * 3) + 
        (team * 2) + 
        RandomNoise
      
      Sol_vec[row_index] <- sol
      Station_vec[row_index] <- station
      Team_vec[row_index] <- team
      Output_vec[row_index] <- Output_value
      
      row_index <- row_index + 1
    }
  }
}

# Create dataframe
production_data <- data.frame(
  Sol = Sol_vec,
  Station = Station_vec,
  Team = Team_vec,
  Output = Output_vec
)

# Display first 10 rows
head(production_data, 10)



# -----------------------------
# TASK B: Total Production per Station
# -----------------------------

station_totals <- data.frame(
  Station = 1:6,
  TotalOutput = numeric(6)
)

for (station in 1:6) {
  
  total_sum <- 0
  
  for (i in 1:nrow(production_data)) {
    
    if (production_data$Station[i] == station) {
      total_sum <- total_sum + production_data$Output[i]
    }
    
  }
  
  station_totals$TotalOutput[station] <- total_sum
}

station_totals



# -----------------------------
# TASK C: Detect Production Anomalies
# -----------------------------

anomalies <- data.frame(
  Sol = numeric(),
  Station = numeric(),
  Team = numeric(),
  Output = numeric(),
  Anomaly = logical()
)

for (sol in 1:30) {
  for (station in 1:6) {
    
    # Calculate daily total for station
    daily_total <- 0
    
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol &&
          production_data$Station[i] == station) {
        daily_total <- daily_total + production_data$Output[i]
      }
    }
    
    daily_avg <- daily_total / 4
    
    # Check each team
    for (i in 1:nrow(production_data)) {
      
      if (production_data$Sol[i] == sol &&
          production_data$Station[i] == station) {
        
        deviation <- abs(production_data$Output[i] - daily_avg) / daily_avg
        
        if (deviation > 0.10) {
          
          anomalies <- rbind(anomalies,
                             data.frame(
                               Sol = production_data$Sol[i],
                               Station = production_data$Station[i],
                               Team = production_data$Team[i],
                               Output = production_data$Output[i],
                               Anomaly = TRUE
                             ))
        }
      }
    }
  }
}

head(anomalies, 10)



# -----------------------------
# TASK D: Identify Peak Productivity Days
# -----------------------------

peak_days <- data.frame(
  Station = numeric(),
  Sol = numeric(),
  TotalOutput = numeric()
)

for (station in 1:6) {
  
  max_output <- -Inf
  peak_sol <- 0
  
  for (sol in 1:30) {
    
    daily_total <- 0
    
    for (i in 1:nrow(production_data)) {
      
      if (production_data$Station[i] == station &&
          production_data$Sol[i] == sol) {
        daily_total <- daily_total + production_data$Output[i]
      }
      
    }
    
    if (daily_total > max_output) {
      max_output <- daily_total
      peak_sol <- sol
    }
  }
  
  peak_days <- rbind(peak_days,
                     data.frame(
                       Station = station,
                       Sol = peak_sol,
                       TotalOutput = max_output
                     ))
}

peak_days


# -----------------------------
# TASK E: Efficiency Score
# -----------------------------

EfficiencyScore_vec <- numeric(nrow(production_data))

for (i in 1:nrow(production_data)) {
  
  EfficiencyScore_vec[i] <- 
    production_data$Output[i] /
    (production_data$Station[i] + production_data$Team[i])
}

production_data$EfficiencyScore <- EfficiencyScore_vec

head(production_data, 10)

