#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 = c("Oviyan","Nithi","Malka","Lavs","Mars","Kavin")
sub1 = c(89,96,94,79,88,92)
sub2 = c(86,79,85,99,68,74)
sub3 = c(89,56,87,99,34,89)
sub4 = c(23,65,45,83,90,95)
sub5 = c(69,75,0,55,76,91)
df = data.frame(Student_Name = Students, Sub1=sub1,Sub2=sub2,Sub3=sub3,Sub4=sub4,Sub5=sub5)
print(df)
## Student_Name Sub1 Sub2 Sub3 Sub4 Sub5
## 1 Oviyan 89 86 89 23 69
## 2 Nithi 96 79 56 65 75
## 3 Malka 94 85 87 45 0
## 4 Lavs 79 99 99 83 55
## 5 Mars 88 68 34 90 76
## 6 Kavin 92 74 89 95 91
#Find the total score of each student. Append a column to include the total score of the students.
df$Total = rowSums(df[,c("Sub1","Sub2","Sub3","Sub4","Sub5")])
df
## Student_Name Sub1 Sub2 Sub3 Sub4 Sub5 Total
## 1 Oviyan 89 86 89 23 69 356
## 2 Nithi 96 79 56 65 75 371
## 3 Malka 94 85 87 45 0 311
## 4 Lavs 79 99 99 83 55 415
## 5 Mars 88 68 34 90 76 356
## 6 Kavin 92 74 89 95 91 441
#Store the details in a comma separated values (csv) file – StudMarks.csv. Also suppress the row numbers.
write.csv(df,"StudMarks.csv")
#Read the content of „StudMarks.csv‟ in a new data frame and view it.
read.csv("StudMarks.csv")
## X Student_Name Sub1 Sub2 Sub3 Sub4 Sub5 Total
## 1 1 Oviyan 89 86 89 23 69 356
## 2 2 Nithi 96 79 56 65 75 371
## 3 3 Malka 94 85 87 45 0 311
## 4 4 Lavs 79 99 99 83 55 415
## 5 5 Mars 88 68 34 90 76 356
## 6 6 Kavin 92 74 89 95 91 441
#Access the scores of students in course 3 using the column name.
print(df$Sub3)
## [1] 89 56 87 99 34 89
#Extract the score of third student in course 4.
print(df[3,5])
## [1] 45
#Extract the scores of the first and second student in all the courses.
print(df[c(1,2),])
## Student_Name Sub1 Sub2 Sub3 Sub4 Sub5 Total
## 1 Oviyan 89 86 89 23 69 356
## 2 Nithi 96 79 56 65 75 371
#Display the names and total scores of all students.
print(df[,c(1,7)])
## Student_Name Total
## 1 Oviyan 356
## 2 Nithi 371
## 3 Malka 311
## 4 Lavs 415
## 5 Mars 356
## 6 Kavin 441
#Make the column “name” as the row index of the data frame.
rownames(df)<-df$Student_Name
print(df)
## Student_Name Sub1 Sub2 Sub3 Sub4 Sub5 Total
## Oviyan Oviyan 89 86 89 23 69 356
## Nithi Nithi 96 79 56 65 75 371
## Malka Malka 94 85 87 45 0 311
## Lavs Lavs 79 99 99 83 55 415
## Mars Mars 88 68 34 90 76 356
## Kavin Kavin 92 74 89 95 91 441
#Display the names of the students those who were present for Course 4.
print(df[which(df$Sub4!=0.0),1])
## [1] "Oviyan" "Nithi" "Malka" "Lavs" "Mars" "Kavin"
#Obtain the names whose total score is above its average.
df$averageMarks=df$Total/5
df
## Student_Name Sub1 Sub2 Sub3 Sub4 Sub5 Total averageMarks
## Oviyan Oviyan 89 86 89 23 69 356 71.2
## Nithi Nithi 96 79 56 65 75 371 74.2
## Malka Malka 94 85 87 45 0 311 62.2
## Lavs Lavs 79 99 99 83 55 415 83.0
## Mars Mars 88 68 34 90 76 356 71.2
## Kavin Kavin 92 74 89 95 91 441 88.2
avg= sum(df$averageMarks)/6
print(df[which(df$averageMarks>avg),1])
## [1] "Lavs" "Kavin"
#Part- B
#The mtcars dataset data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 #automobiles (1973–74 models). #It is a data frame with 32 observations on 11 (numeric) variables with model names as rownames. #[, 1] mpg Miles/(US) gallon #[, 2] cyl Number of cylinders #[, 3] disp Displacement (cu.in.) #[, 4] hp Gross horsepower #[, 5] drat Rear axle ratio #[, 6] wt Weight (1000 lbs) #[, 7] qsec 1/4 mile time #[, 8] vs Engine (0 = V-shaped, 1 = straight) #[, 9] am Transmission (0 = automatic, 1 = manual) #,10] gear Number of forward gears #[,11] carb Number of carburetors
#Make a copy of mtcars in a new variable and implement the following:
data(mtcars)
#Display the structure and dimension of mtcars
dim(mtcars)
## [1] 32 11
structure(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#Find the car models that have the highest and the lowest horse power.
mtcars$car_name <- rownames(mtcars)
high_hp <- mtcars[which.max(mtcars$hp), ]
high_hp_model <- high_hp$car_name
high_hp_hp <- high_hp$hp
cat(high_hp_model,high_hp_hp)
## Maserati Bora 335
low_hp <- mtcars[which.min(mtcars$hp), ]
low_hp_model <- low_hp$car_name
low_hp_hp <- low_hp$hp
cat(low_hp_model, low_hp_hp)
## Honda Civic 52
#Display the names of all the automobile models listed in mtcars
car_names<-mtcars$car_name
print(car_names)
## [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.
avg_hp <- mean(mtcars$hp)
high_hp_cars <- mtcars[mtcars$hp > avg_hp, "car_name"]
print(high_hp_cars)
## [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.
automatic_cars <- mtcars[mtcars$am == 0, ]
num_automatic_cars <- nrow(automatic_cars)
automatic_car_names <- automatic_cars$car_name
print(num_automatic_cars)
## [1] 19
print(automatic_car_names)
## [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 <- as.Date("2003-11-06",format = "%Y-%m-%d")
dt
## [1] "2003-11-06"
#Convert the type of dt to Date() and store it in a variable and print its type.
dt_date <- as.Date(dt)
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(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(class(dt_posixlt))
## [1] "POSIXlt" "POSIXt"
#Print the weekday, month and quarter of your DoB.
weekday <- format(dt, "%A")
month <- as.numeric(format(dt, "%m"))
quarter <- ceiling(month / 3)
print(paste("Weekday:", weekday))
## [1] "Weekday: Thursday"
print(paste("Month:", month))
## [1] "Month: 11"
print(paste("Quarter:", quarter))
## [1] "Quarter: 4"
#Generate and display 5 consecutive days from your DoB.
consecutive_days <- seq(dt, by = "day", length.out = 5)
print(consecutive_days)
## [1] "2003-11-06" "2003-11-07" "2003-11-08" "2003-11-09" "2003-11-10"
#Print today’s date and time.
print(Sys.Date())
## [1] "2024-08-03"
print(Sys.time())
## [1] "2024-08-03 15:55:51 IST"
#Generate and display 5 new dates from your DoB with a distance of 3 months.
new_dates <- seq(dt, by = "3 months", length.out = 5)
print(new_dates)
## [1] "2003-11-06" "2004-02-06" "2004-05-06" "2004-08-06" "2004-11-06"
#Display your age in year and months. [eg: 20 years and 4 months].
current_date <- Sys.Date()
years <- as.numeric(format(current_date, "%Y")) - as.numeric(format(dt, "%Y"))
months <- as.numeric(format(current_date, "%m")) - as.numeric(format(dt, "%m"))
if (months < 0) {
years <- years - 1
months <- months + 12
}
print(paste(years, "years and", months, "months"))
## [1] "20 years and 9 months"