PART - A
#Create a data frame with 6 students and the marks scored by them in 5 different courses. Implement the following:
# Each course has a maximum score of 100. If a student is present for the exam, its entry contains the score value and 0 otherwise.
students <- data.frame(
name = c("Kaarthika", "Praba", "Divya", "Thiru", "Sudharshan", "Ramya"),
course1 = c(85, 90, 78, 0, 92, 88),
course2 = c(76, 0, 89, 85, 0, 91),
course3 = c(0, 67, 80, 88, 94, 0),
course4 = c(88, 90, 0, 92, 77, 84),
course5 = c(95, 85, 87, 0, 83, 90)
)
print(students)
## name course1 course2 course3 course4 course5
## 1 Kaarthika 85 76 0 88 95
## 2 Praba 90 0 67 90 85
## 3 Divya 78 89 80 0 87
## 4 Thiru 0 85 88 92 0
## 5 Sudharshan 92 0 94 77 83
## 6 Ramya 88 91 0 84 90
#Find the total score of each student. Append a column to include the total score of the students.
students$total <- rowSums(students[ , 2:6])
print(students)
## name course1 course2 course3 course4 course5 total
## 1 Kaarthika 85 76 0 88 95 344
## 2 Praba 90 0 67 90 85 332
## 3 Divya 78 89 80 0 87 334
## 4 Thiru 0 85 88 92 0 265
## 5 Sudharshan 92 0 94 77 83 346
## 6 Ramya 88 91 0 84 90 353
#Store the details in a comma separated values (csv) file – StudMarks.csv. Also suppress the row numbers.
write.csv(students, "StudMarks.csv", row.names = FALSE)
#Read the content of „StudMarks.csv‟ in a new data frame and view it.
new_students <- read.csv("StudMarks.csv")
print(new_students)
## name course1 course2 course3 course4 course5 total
## 1 Kaarthika 85 76 0 88 95 344
## 2 Praba 90 0 67 90 85 332
## 3 Divya 78 89 80 0 87 334
## 4 Thiru 0 85 88 92 0 265
## 5 Sudharshan 92 0 94 77 83 346
## 6 Ramya 88 91 0 84 90 353
#Access the scores of students in course3 using the column name.
course3_scores <- new_students$course3
print(course3_scores)
## [1] 0 67 80 88 94 0
#Extract the score of third student in course4.
third_student_course4 <- new_students[3, "course4"]
print(third_student_course4)
## [1] 0
#Extract the scores of the first and second student in all the courses.
first_second_scores <- new_students[1:2, 2:6]
print(first_second_scores)
## course1 course2 course3 course4 course5
## 1 85 76 0 88 95
## 2 90 0 67 90 85
#Display the names and total scores of all students
names_total_scores <- new_students[ , c("name", "total")]
print(names_total_scores)
## name total
## 1 Kaarthika 344
## 2 Praba 332
## 3 Divya 334
## 4 Thiru 265
## 5 Sudharshan 346
## 6 Ramya 353
# Make the column “name” as the row index of the data frame.
row.names(new_students) <- new_students$name
new_students <- new_students[ , -1] # Remove the original name column
print(new_students)
## course1 course2 course3 course4 course5 total
## Kaarthika 85 76 0 88 95 344
## Praba 90 0 67 90 85 332
## Divya 78 89 80 0 87 334
## Thiru 0 85 88 92 0 265
## Sudharshan 92 0 94 77 83 346
## Ramya 88 91 0 84 90 353
#Display the names of the students those who were present for Course4.
present_course4 <- row.names(new_students[new_students$course4 > 0, ])
print(present_course4)
## [1] "Kaarthika" "Praba" "Thiru" "Sudharshan" "Ramya"
#Obtain the names whose total score is above its average.
average_total <- mean(new_students$total)
above_average <- row.names(new_students[new_students$total > average_total, ])
print(above_average)
## [1] "Kaarthika" "Praba" "Divya" "Sudharshan" "Ramya"
Part - B
#Make a copy of mtcars in a new variable and implement the following:
#Display the structure and dimension of mtcars
mtcars_copy <- 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 ...
dim(mtcars_copy)
## [1] 32 11
#Find the car models that have the highest and the lowest horse power.
max_hp_model <- rownames(mtcars_copy)[which.max(mtcars_copy$hp)]
min_hp_model <- rownames(mtcars_copy)[which.min(mtcars_copy$hp)]
max_hp_model
## [1] "Maserati Bora"
min_hp_model
## [1] "Honda Civic"
#Display the names of all the automobile models listed in mtcars
car_models <- rownames(mtcars_copy)
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"
#Identify the car models whose horse power is greater than the average horse power.
average_hp <- mean(mtcars_copy$hp)
high_hp_models <- rownames(mtcars_copy)[mtcars_copy$hp > average_hp]
high_hp_models
## [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"
# How many cars have automatic transmission? Display their names.
auto_cars <- mtcars_copy[mtcars_copy$am == 0, ]
num_auto_cars <- nrow(auto_cars)
auto_car_models <- rownames(auto_cars)
num_auto_cars
## [1] 19
auto_car_models
## [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-11-29"
#Convert the type of dt to Date() and store it in a variable and print its type
dt_date <- as.Date(dt)
print(dt_date)
## [1] "2003-11-29"
print(class(dt_date))
## [1] "Date"
#Convert the type of dt to POSIXct() and store it in a variable and print its type.
dt_posixct <- as.POSIXct(dt)
print(dt_posixct)
## [1] "2003-11-29 IST"
print(class(dt_posixct))
## [1] "POSIXct" "POSIXt"
#Convert the type of dt to POSIXlt() and store it in a variable and print its type
dt_posixlt <- as.POSIXlt(dt)
print(dt_posixlt)
## [1] "2003-11-29 IST"
print(class(dt_posixlt))
## [1] "POSIXlt" "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: Saturday"
print(paste("Month:", month))
## [1] "Month: November"
print(paste("Quarter:", quarter))
## [1] "Quarter: Q4"
#Generate and display 5 consecutive days from your DoB.
consecutive_days <- seq.Date(from = dt_date, by = "day", length.out = 5)
print(consecutive_days)
## [1] "2003-11-29" "2003-11-30" "2003-12-01" "2003-12-02" "2003-12-03"
#Print today’s date and time.
current_datetime <- Sys.time()
print(current_datetime)
## [1] "2024-08-04 18:04:16 IST"
#Generate and display 5 new dates from your DoB with a distance of 3 months.
new_dates <- seq.Date(from = dt_date, by = "3 months", length.out = 5)
print(new_dates)
## [1] "2003-11-29" "2004-02-29" "2004-05-29" "2004-08-29" "2004-11-29"
#Display your age in year and months.
current_date <- Sys.Date()
age_diff <- as.numeric(difftime(current_date, dt_date, units = "days"))
age_years <- as.integer(age_diff / 365.25)
age_months <- as.integer((age_diff %% 365.25) / 30.44)
print(paste(age_years, "years and", age_months, "months"))
## [1] "20 years and 8 months"