PART A: Create a data frame with 6 students and the marks scored by them in 5 different courses. Implement the following:

# Create the data frame with student names and marks
students=data.frame(
  name = c("Jai", "Hema", "Anu", "Harini", "Shara", "Kanss"),
  course1 = c(85, 90, 78, 92, 0, 88),
  course2 = c(0, 85, 80, 87, 90, 95),
  course3 = c(90, 92, 0, 85, 88, 90),
  course4 = c(85, 0, 88, 92, 86, 84),
  course5 = c(88, 85, 90, 0, 92, 87)
)
print(students)
##     name course1 course2 course3 course4 course5
## 1    Jai      85       0      90      85      88
## 2   Hema      90      85      92       0      85
## 3    Anu      78      80       0      88      90
## 4 Harini      92      87      85      92       0
## 5  Shara       0      90      88      86      92
## 6  Kanss      88      95      90      84      87
# Calculate the total score for each student and append it as a new column
students$total_score=rowSums(students[, 2:6])
print(students)
##     name course1 course2 course3 course4 course5 total_score
## 1    Jai      85       0      90      85      88         348
## 2   Hema      90      85      92       0      85         352
## 3    Anu      78      80       0      88      90         336
## 4 Harini      92      87      85      92       0         356
## 5  Shara       0      90      88      86      92         356
## 6  Kanss      88      95      90      84      87         444
# Write the data frame to a CSV file without row numbers
write.csv(students, file = "StudMarks.csv", row.names = FALSE)
# Read the content of "StudMarks.csv" in a new data frame and view it
students_df=read.csv("StudMarks.csv")
View(students_df)
# Access the scores of students in course3 using the column name
course3_scores=students_df$course3
print(course3_scores)
## [1] 90 92  0 85 88 90
# Extract the score of the third student in course4
third_student_course4=students_df[3, "course4"]
print(third_student_course4)
## [1] 88
# Extract the scores of the first and second student in all the courses
first_second_students_scores=students_df[1:2, 2:6]
print(first_second_students_scores)
##   course1 course2 course3 course4 course5
## 1      85       0      90      85      88
## 2      90      85      92       0      85
# Display the names and total scores of all students
names_total_scores=students_df[, c("name", "total_score")]
print(names_total_scores)
##     name total_score
## 1    Jai         348
## 2   Hema         352
## 3    Anu         336
## 4 Harini         356
## 5  Shara         356
## 6  Kanss         444
# Make the column “name” as the row index of the data frame
rownames(students_df)=students_df$name
students_df=students_df[, -1]
# Display the names of the students those who were present for Course4
present_course4= rownames(students_df[students_df$course4 > 0, ])
print(present_course4)
## [1] "Jai"    "Anu"    "Harini" "Shara"  "Kanss"
# Obtain the names whose total score is above its average
average_total_score=mean(students_df$total_score)
above_average_students= rownames(students_df[students_df$total_score > average_total_score, ])
print(above_average_students)
## [1] "Kanss"

PART B:

# Load the mtcars dataset
data(mtcars)

# Make a copy of mtcars in a new variable
mtcars_copy <- mtcars
# Display the structure of mtcars
str(mtcars_copy)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
# Display the dimension of mtcars
dim(mtcars_copy)
## [1] 32 11
# Find the car model with the highest horse power
max_hp=max(mtcars_copy$hp)
car_max_hp=rownames(mtcars_copy[mtcars_copy$hp == max_hp, ])
print(paste("Car with the highest horsepower:", car_max_hp))
## [1] "Car with the highest horsepower: Maserati Bora"
# Find the car model with the lowest horse power
min_hp=min(mtcars_copy$hp)
car_min_hp=rownames(mtcars_copy[mtcars_copy$hp == min_hp, ])
print(paste("Car with the lowest horsepower:", car_min_hp))
## [1] "Car with the lowest horsepower: Honda Civic"
# Display the names of all the automobile models listed in mtcars
car_models=rownames(mtcars_copy)
print(car_models)
##  [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"         
##  [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
##  [7] "Duster 360"          "Merc 240D"           "Merc 230"           
## [10] "Merc 280"            "Merc 280C"           "Merc 450SE"         
## [13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
## [16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"           
## [19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"      
## [22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
## [25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"      
## [28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
## [31] "Maserati Bora"       "Volvo 142E"
# Calculate the average horse power
average_hp=mean(mtcars_copy$hp)

# Identify the car models whose horse power is greater than the average horse power
cars_above_avg_hp=rownames(mtcars_copy[mtcars_copy$hp > average_hp, ])
print("Car models with horse power greater than the average:")
## [1] "Car models with horse power greater than the average:"
print(cars_above_avg_hp)
##  [1] "Hornet Sportabout"   "Duster 360"          "Merc 450SE"         
##  [4] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
##  [7] "Lincoln Continental" "Chrysler Imperial"   "Dodge Challenger"   
## [10] "AMC Javelin"         "Camaro Z28"          "Pontiac Firebird"   
## [13] "Ford Pantera L"      "Ferrari Dino"        "Maserati Bora"
# Identify the number of cars with automatic transmission
num_automatic_cars=sum(mtcars_copy$am == 0)

# Display the names of cars with automatic transmission
automatic_cars=rownames(mtcars_copy[mtcars_copy$am == 0, ])
print(paste("Number of cars with automatic transmission:", num_automatic_cars))
## [1] "Number of cars with automatic transmission: 19"
print("Names of cars with automatic transmission:")
## [1] "Names of cars with automatic transmission:"
print(automatic_cars)
##  [1] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
##  [4] "Duster 360"          "Merc 240D"           "Merc 230"           
##  [7] "Merc 280"            "Merc 280C"           "Merc 450SE"         
## [10] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
## [13] "Lincoln Continental" "Chrysler Imperial"   "Toyota Corona"      
## [16] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
## [19] "Pontiac Firebird"

PART C:

# Store your date of birth in a variable dt
dt = "2003-07-01"
# Convert the type of dt to Date() and store it in a variable
dt_date=as.Date(dt)

# Print its type
print(paste("Type of dt_date:", class(dt_date)))
## [1] "Type of dt_date: Date"
# Convert the type of dt to POSIXct() and store it in a variable
dt_posixct=as.POSIXct(dt)

# Print its type
print(paste("Type of dt_posixct:", class(dt_posixct)))
## [1] "Type of dt_posixct: POSIXct" "Type of dt_posixct: POSIXt"
# Print the weekday, month and quarter of your DoB
weekday = weekdays(dt_date)
month = months(dt_date)
quarter = quarters(dt_date)

print(paste("Weekday:", weekday))
## [1] "Weekday: Tuesday"
print(paste("Month:", month))
## [1] "Month: July"
print(paste("Quarter:", quarter))
## [1] "Quarter: Q3"
# Generate and display 5 consecutive days from your DoB
consecutive_days =dt_date + 0:4
print("5 consecutive days from DoB:")
## [1] "5 consecutive days from DoB:"
print(consecutive_days)
## [1] "2003-07-01" "2003-07-02" "2003-07-03" "2003-07-04" "2003-07-05"
# Print today’s date and time
today= Sys.time()
print(paste("Today's date and time:", today))
## [1] "Today's date and time: 2024-08-04 18:30:19.381691"
# Generate and display 5 new dates from your DoB with a distance of 3 months
new_dates=seq(dt_date, by = "3 months", length.out = 5)
print("5 new dates with a distance of 3 months from DoB:")
## [1] "5 new dates with a distance of 3 months from DoB:"
print(new_dates)
## [1] "2003-07-01" "2003-10-01" "2004-01-01" "2004-04-01" "2004-07-01"
# Calculate age in years and months
current_date=Sys.Date()
age_years=as.numeric(difftime(current_date, dt_date, units = "weeks")) %/% 52
age_months= (as.numeric(difftime(current_date, dt_date, units = "weeks")) %% 52) %/% 4

print(paste("Age:", age_years, "years and", age_months, "months"))
## [1] "Age: 21 years and 2 months"