Question 1

a <- c(T,T,T,T,F,F,F,F)
b <- c(T,T,F,F,T,T,F,F)
c <- c(T,F,T,F,T,F,T,F)

d <- a&b
e <- (a&b)|c
f <- a&(b|c)
g <- a&(!b|c)
h <- a&!(b|c)
i <- !a&!(b|c)

d
## [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
e
## [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE
f
## [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
g
## [1]  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
h
## [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
i
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

Question 2

The first function is checking to see if the input is even and if it is then it is adding it to the output vector. The function requires a numeric array, so a string or character would not work. The corresponding output is an array of all the even numbers from the input function.

The second block of code is actually two functions. The first function is checking to see if the input number is a prime number or not. The second function labeled function2, is checking to see how many prime numbers are in the numbers from 1 to the given input. For both functions in this block of code, we can expect an input of a single integer number. The output of the IsPrime function will be a TRUE or FALSE. The output of function2 will be a single integer showing how many prime numbers are between 1 and the input.

2a.

String Sum Function

String_Sum_Function <- function(input) {
  input <- abs(input) 
  Character_Conversion <- as.character(input) 
  String_Split <- strsplit(Character_Conversion, split = "") 
  digits <- unlist(String_Split) 
  digits <- as.numeric(digits) 
  return(sum(digits))
}

String_Sum_Function(-16)
## [1] 7

Question 2b.

Modulus Sum Function

Modulus_Sum_Function <-function(input2) {
  input2 <- abs(input2)  # In case of negative numbers
  sum <- 0 #set up sum value
  while (input2 > 0) { #while there are still digits in the input
    sum <- sum + (input2 %% 10) #adds the last digit to the sum
    input2 <- input2 %/% 10  # removes the last digit from the input
  }
  return(sum)
}

Modulus_Sum_Function(-88)
## [1] 16

Question 3

fuelingData <- read.table("fuelingData.txt", header = FALSE)



for (i in 1:nrow(fuelingData)) {
  if (fuelingData$V3[i] == "mi" && fuelingData$V5[i] == "gal") {
   
    Loop_Value <- fuelingData$V2[i]
    fuelingData$V2[i] <- fuelingData$V4[i]
    fuelingData$V4[i] <- Loop_Value
    

    Loop_Value2 <- fuelingData$V3[i]
    fuelingData$V3[i] <- fuelingData$V5[i]
    fuelingData$V5[i] <- Loop_Value2
  }
}




fuelingData$Odometer_Working <- TRUE  # assume true

for (i in 1:nrow(fuelingData)) { # Go through each row
  # Check if any of the 5 columns have a ? in them
  if (grepl("\\?", fuelingData$V1[i]) ||
      grepl("\\?", fuelingData$V2[i]) ||
      grepl("\\?", fuelingData$V3[i]) ||
      grepl("\\?", fuelingData$V4[i]) ||
      grepl("\\?", fuelingData$V5[i])) {
    
    # If there's a question mark, set to FALSE
    fuelingData$Odometer_Working[i] <- FALSE
  }
}




fuelingData$V1 <- gsub("gal", "", fuelingData$V1)
fuelingData$V1 <- gsub("\\?", "", fuelingData$V1)
fuelingData$V1 <- gsub("[^0-9.-]", "", fuelingData$V1)
fuelingData$V1 <- as.numeric(fuelingData$V1)

fuelingData$V2 <- as.numeric(fuelingData$V2)

fuelingData$V4 <- gsub("\\?", "", fuelingData$V4)
fuelingData$V4 <- as.numeric(fuelingData$V4)


fuelingData$MPG <- fuelingData$V4 / fuelingData$V2


fuelingData$Fuel_Up_Cost <- fuelingData$V1 * fuelingData$V2


Total_Gallons <- sum(fuelingData$V2)
Total_Mileage <- sum(fuelingData$V4)
Total_Fuel_Cost <- sum(fuelingData$Fuel_Up_Cost)
Average_MPG <- (sum(fuelingData$MPG))/nrow(fuelingData)

Total_Gallons
## [1] 1514.765
Total_Mileage
## [1] 37257.8
Total_Fuel_Cost
## [1] 4297.94
Average_MPG
## [1] 24.89156
plot(fuelingData$V2, fuelingData$V4,
     main = "Scatterplot of Gallons consumed vs Miles Travelled", 
     xlab = "Gasoline (Gal)", 
     ylab = "Miles",
     )

There are a lot of outliers due to the odometer not working.

Question 4

fuelingData_Claire <- read.table("fuelingData_Claire.txt", header = FALSE, fill
                                 = TRUE, stringsAsFactors = FALSE)


fuelingData_Claire$V8[1] <- "mpg" #fix incorrect units and missing data
fuelingData_Claire$V8[2] <- "mpg"
fuelingData_Claire$V7[1] <- fuelingData_Claire$V5[1]/fuelingData_Claire$V3[1]
fuelingData_Claire$V7[2] <- fuelingData_Claire$V5[2]/fuelingData_Claire$V3[2]
fuelingData_Claire$V8[19] <- "mpg"
fuelingData_Claire$V8[20] <- "mpg" 

fuelingData_Claire$V2 <- gsub("\\$", "", fuelingData_Claire$V2)     
fuelingData_Claire$V2 <- gsub("/gal", "", fuelingData_Claire$V2)    
fuelingData_Claire$V2 <- as.numeric(fuelingData_Claire$V2)         
fuelingData_Claire$V1 <- as.Date(fuelingData_Claire$V1, format = "%m/%d/%Y")



fuelingData_Claire$MPG <- fuelingData_Claire$V5/fuelingData_Claire$V3


Fuel_Up_Cost_Claire <- sum(fuelingData_Claire$V2*fuelingData_Claire$V3) #total fuel cost
CostPerMile_Claire <- Fuel_Up_Cost_Claire/sum(fuelingData_Claire$V5) 

Fuel_Up_Cost_Doris <- Total_Fuel_Cost
CostPerMile_Doris <- Fuel_Up_Cost_Doris/Total_Mileage

CostPerMile_Claire
## [1] 0.001921637
CostPerMile_Doris
## [1] 0.1153568

Claire is much more fuel efficient with a combination of electricity and gas

Question 5

mackayla_trips <- read.csv("mackayla_trips_clean.csv", stringsAsFactors = FALSE)
mackayla_charging <- read.csv("mackayla_charging_clean.csv", stringsAsFactors = FALSE)

#create a datetime column from Date and Start.Time
mackayla_trips$Start.DateTime <- mdy_hm(paste(mackayla_trips$Date, mackayla_trips$Start.Time))

mackayla_charging$Start.DateTime <- mdy_hm(paste(mackayla_charging$Date, mackayla_charging$Start.Time))
mackayla_charging$End.DateTime <- mdy_hm(paste(mackayla_charging$Date, mackayla_charging$End.Time))

#convert columns to numeric - charging File
mackayla_charging$Start.Perc <- as.numeric(gsub("%", "", mackayla_charging$Start.Perc)) / 100
mackayla_charging$End.Perc <- as.numeric(gsub("%", "", mackayla_charging$End.Perc)) / 100
mackayla_charging$Charge.Added <- as.numeric(gsub("%", "", mackayla_charging$Charge.Added)) / 100
mackayla_charging$Distance.Added <- as.numeric(mackayla_charging$Distance.Added)
mackayla_charging$Charge.Time..min <- as.numeric(mackayla_charging$Charge.Time..min)

mackayla_charging$Cost <- as.numeric(gsub("[$]", "", mackayla_charging$Cost))


#convert columns to numeric - trips File
mackayla_trips$Distance <- as.numeric(mackayla_trips$Distance)
mackayla_trips$Energy.Used <- as.numeric(mackayla_trips$Energy.Used)
mackayla_trips$Energy.Efficiency <- as.numeric(mackayla_trips$Energy.Efficiency)
mackayla_trips$Brake.Score <- as.numeric(gsub("%", "", mackayla_trips$Brake.Score))/100
mackayla_trips$Brake.Regen <- as.numeric(mackayla_trips$Brake.Regen)
mackayla_trips$Driving.Score <- as.numeric(gsub("%", "", mackayla_trips$Driving.Score))/100


Total_Mileage_MackaylaCharging <- sum(mackayla_charging$Distance.Added)
Total_Mileage_MackaylaTrips <- sum(mackayla_trips$Distance)
Total_Money_Spent_Mackayla <- sum(mackayla_charging$Cost)
CostPerMile_Mackayla <- Total_Money_Spent_Mackayla/Total_Mileage_MackaylaTrips

CostPerMile_Claire
## [1] 0.001921637
CostPerMile_Doris
## [1] 0.1153568
CostPerMile_Mackayla
## [1] 0.02998406
plot(mackayla_trips$Distance, mackayla_trips$Energy.Used,
     main = "Mileage vs Energy Used with Mackayla", 
     xlab = "Distance Travelled (mi)", 
     ylab = "Energy Used",
)

hist(mackayla_trips$Energy.Efficiency, 
     breaks = 10)

hist(mackayla_trips$Energy.Efficiency[mackayla_trips$Energy.Efficiency < 10],
     breaks = 30,
     main = "Histogram of Energy Efficiency (< 10)",
     xlab = "Energy Efficiency",)

Claire is the most efficient in terms of cost per mileage, followed by Mackayla and Doris respectively

There are outliers in the data. This causes a need to restrict the values to be under 10 to properly view the data. If Energy.Used is correct then, either distance is incorrect or there are a couple instances where very little energy is used to go far distances (for example: going downhill for an extended period of time and charging the battery)