#Reasha Vora
#4/15/24
library(stringr)
## Warning: package 'stringr' was built under R version 4.3.3
## 1.1.1
## Create a vector ‘movies‘ that contains the names of six movies you like. Print it to show it
## looks good.
print("1.1.1")
## [1] "1.1.1"
movies <- c("Black Panther: Wakanda Forever", "Jurassic World Dominion", "The Batman", "Forrest Gump", "The Matrix", "Titanic")
cat(movies, "\n", sep = ", ")
## Black Panther: Wakanda Forever, Jurassic World Dominion, The Batman, Forrest Gump, The Matrix, Titanic,
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.1.2
print("1.1.2")
## [1] "1.1.2"
firstThreeMovies <- movies[1:3]
cat(firstThreeMovies, "\n", sep = ", ")
## Black Panther: Wakanda Forever, Jurassic World Dominion, The Batman,
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.1.3
print("1.1.3")
## [1] "1.1.3"
moviesWithPhrase <- str_c(movies, "is a great movie!", sep = " ")
cat(moviesWithPhrase, "\n", sep = ", ")
## Black Panther: Wakanda Forever is a great movie!, Jurassic World Dominion is a great movie!, The Batman is a great movie!, Forrest Gump is a great movie!, The Matrix is a great movie!, Titanic is a great movie!,
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.1.4
print("1.1.4")
## [1] "1.1.4"
updatedMovies <- movies[-4]
cat(updatedMovies, "\n", sep = ", ")
## Black Panther: Wakanda Forever, Jurassic World Dominion, The Batman, The Matrix, Titanic,
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.1.5
numbers_40_to_49 <- 40:49

## 1.1.6
print("1.1.6")
## [1] "1.1.6"
numbersLength <- length(numbers_40_to_49)
cat("        Length of numbers: ",numbersLength, "\n", sep = "")
##         Length of numbers: 10
#Displays 10, total numbers from 40 to 49 is 10
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.1.7
mean_of_numbers <- mean(numbers_40_to_49)

## 1.1.8
numbers_70_to_79 <- 70:79


## 1.1.9
combined_numbers <- c(numbers_40_to_49, numbers_70_to_79)
## 1.2.1
print("1.2.1")
## [1] "1.2.1"
iLogical <- combined_numbers > 70
print(iLogical)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
## [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.2.2
print("1.2.2")
## [1] "1.2.2"
numbers_larger_than_70 <- combined_numbers[iLogical]
print(numbers_larger_than_70)
## [1] 71 72 73 74 75 76 77 78 79
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.2.3
## Compute sum of your iLogical vector. Explain what do you get, and what does it mean.
## (You can write explanations in code comments).
## The logical vector is counting the number of TRUE values.
## the iLogical vector contains TRUE for elements larger than 70 and FALSE otherwise.
## Therefore, the sum_of_iLogical will give the count of numbers larger than 70 in the combined_numbers vector.
sum_of_iLogical <- sum(iLogical)

## 1.2.4
extracted_numbers <- combined_numbers[iLogical]

## 1.2.5
length_of_subset <- length(extracted_numbers)

## 1.2.6
numbers_not_exceeding_70 <- combined_numbers[!iLogical]


## 1.2.7
print("1.2.7")
## [1] "1.2.7"
print(numbers_not_exceeding_70^2)
##  [1] 1600 1681 1764 1849 1936 2025 2116 2209 2304 2401 4900
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.2.8
is_even <- combined_numbers %% 2 == 0


## 1.2.9
print("## 1.2.9")
## [1] "## 1.2.9"
print(combined_numbers[is_even])
##  [1] 40 42 44 46 48 70 72 74 76 78
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.3.1
numbers_negative_to_positive <- -5:5


## 1.3.2
numbers_negative_to_positive[numbers_negative_to_positive < 0] <- 0


## 1.3.3
ReLU <- function(x) {
  x <- ifelse(x > 0, x, 0)
  return(x)
}

ReLU <- function(x) {
  positive_indices <- x > 0
  x[!positive_indices] <- 0
  return(x)
}
sampleVector <- c(-3, -2, -1, 0, 1, 2, 3)
modifiedVector <- ReLU(sampleVector)

## 1.3.4
print("1.3.4")
## [1] "1.3.4"
# Example vectors
exampleVector1 <- c(-3, -2, -1, 0, 1, 2, 3)
exampleVector2 <- c(5, -4, 7, -2, 0, 10, -6)
exampleVector3 <- c(-1, 0, -2, 3, -4, 5)

modifiedVector1 <- ReLU(exampleVector1)
modifiedVector2 <- ReLU(exampleVector2)
modifiedVector3 <- ReLU(exampleVector3)

cat("Example Vector 1:\nOriginal:", exampleVector1, "\ Changed:", modifiedVector1, "\n\n")
## Example Vector 1:
## Original: -3 -2 -1 0 1 2 3  Changed: 0 0 0 0 1 2 3
cat("Example Vector 2:\nOriginal:", exampleVector2, "\ Changed:", modifiedVector2, "\n\n")
## Example Vector 2:
## Original: 5 -4 7 -2 0 10 -6  Changed: 5 0 7 0 0 10 0
cat("Example Vector 3:\nOriginal:", exampleVector3, "\ Changed:", modifiedVector3, "\n")
## Example Vector 3:
## Original: -1 0 -2 3 -4 5  Changed: 0 0 0 3 0 5
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.3.5
print("1.3.5")
## [1] "1.3.5"
absVal <- function(x) {
  x <- ifelse(x < 0, -x, x)
  return(x)
}

exampleVector1 <- c(-3, -2, -1, 0, 1, 2, 3)
exampleVector2 <- c(5, -4, 7, -2, 0, 10, -6)
exampleVector3 <- c(-1, 0, -2, 3, -4, 5)

absVector1 <- absVal(exampleVector1)
absVector2 <- absVal(exampleVector2)
absVector3 <- absVal(exampleVector3)

print("Modified vector 1:")
## [1] "Modified vector 1:"
print(absVector1)
## [1] 3 2 1 0 1 2 3
print("Modified vector 2:")
## [1] "Modified vector 2:"
print(absVector2)
## [1]  5  4  7  2  0 10  6
print("Modified vector 3:")
## [1] "Modified vector 3:"
print(absVector3)
## [1] 1 0 2 3 4 5
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.4.1
students <- c("Bao-yu", "Dai-yu", "Tan-chun", "Xi-chun")
incomes <- c(30000, 60000, 90000, 120000)

## 1.4.2
print("1.4.2")
## [1] "1.4.2"
support <- ifelse(incomes < 66000, 1/3 * (66000 - incomes), 0)
eligibleStudents <- students[support > 0]
print(eligibleStudents)
## [1] "Bao-yu" "Dai-yu"
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.4.3
print("1.4.3")
## [1] "1.4.3"
support_df <- data.frame(Student = students, Support_Amount = support)
print(support_df)
##    Student Support_Amount
## 1   Bao-yu          12000
## 2   Dai-yu           2000
## 3 Tan-chun              0
## 4  Xi-chun              0
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 1.4.4
print("1.4.4")
## [1] "1.4.4"
for (i in seq_along(students)) {
  cat(students[i], ": ", support[i], "\n")
}
## Bao-yu :  12000 
## Dai-yu :  2000 
## Tan-chun :  0 
## Xi-chun :  0
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 2.1
country <- list (
  name = "Germany",
  capital = "Berlin",
  population = 1000000,
  continent = "Europe"
)

## 2.2
print("2.2")
## [1] "2.2"
componentNames <- names(country)
print(componentNames)
## [1] "name"       "capital"    "population" "continent"
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 2.3
capital <- country$capital


## 2.4
population <- country[["population"]]

## 2.5
continentVar <- "continent"
continent <- country[[continentVar]]


## 2.6
print("2.6")
## [1] "2.6"
componentName <- "area"

country[[componentName]] <- 500000  # Just an example value for area

print(country)
## $name
## [1] "Germany"
## 
## $capital
## [1] "Berlin"
## 
## $population
## [1] 1e+06
## 
## $continent
## [1] "Europe"
## 
## $area
## [1] 5e+05
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 3.1.1
classify_age <- function(age) {
  if (age < 18) {
    print("child")
  } else if (age >= 18 && age <= 65) {
    print("working age")
  } else {
    print("retired")
  }
}

## 3.1.2
print("3.1.2")
## [1] "3.1.2"
print("12 yrs old: ")
## [1] "12 yrs old: "
classify_age(12)
## [1] "child"
print("            ")
## [1] "            "
print("18 yrs old: ")
## [1] "18 yrs old: "
classify_age(18)
## [1] "working age"
print("            ")
## [1] "            "
print("20 yrs old: ")
## [1] "20 yrs old: "
classify_age(20)
## [1] "working age"
print("            ")
## [1] "            "
print("64 yrs old: ")
## [1] "64 yrs old: "
classify_age(64)
## [1] "working age"
print("            ")
## [1] "            "
print("70 yrs old: ")
## [1] "70 yrs old: "
classify_age(70)
## [1] "retired"
print("            ")
## [1] "            "
## 3.2.1
classify_group_age <- function(ages) {
  if (all(ages >= 18 & ages <= 65)) {
    print("everyone in working age")
  } else if (any(ages >= 18 & ages <= 65)) {
    print("some people are in working age")
  } else {
    print("no working age people here")
  }
}

## 3.2.2
# Test cases
group1 <- c(22, 33, 44)
group2 <- c(15, 17, 20)
group3 <- c(11, 17, 68)
print("## 3.2.2")
## [1] "## 3.2.2"
print("c(22, 33, 44)")
## [1] "c(22, 33, 44)"
classify_group_age(group1)
## [1] "everyone in working age"
print("-----------------")
## [1] "-----------------"
print("c(15, 17, 20)")
## [1] "c(15, 17, 20)"
classify_group_age(group2)
## [1] "some people are in working age"
print("-----------------")
## [1] "-----------------"
print("c(11, 17, 68)")
## [1] "c(11, 17, 68)"
classify_group_age(group3)
## [1] "no working age people here"
print("-----------------")
## [1] "-----------------"
print("-----------------------------------------------")
## [1] "-----------------------------------------------"
## 3.3.1
ages <- c(12, 22, 14, 44, 17, 18)
ageGroups <- ifelse(ages < 18, "child", "adult")

## 3.3.2
print("3.3.2")
## [1] "3.3.2"
ReLU <- function(x) {
  return(ifelse(x > 0, x, 0))
}
x <- c(-3, -1, 0, 2, 5)
print(ReLU(x))
## [1] 0 0 0 2 5
## 3.3.3
abs1 <- function(x) {
  ifelse(x >= 0, x, -x)
}

x <- c(-3, -1, 0, 2, 5)
print(abs1(x))
## [1] 3 1 0 2 5
print("-----------------------------------------------")
## [1] "-----------------------------------------------"