ASSIGNMENT NO 7
Data Analysis
Practice Program:
1. Using inbuilt dataset women perform the following
a. displays all rows of dataset having greater than 120
b. displays all rows of dataset in ascending order of weight.
Solution:
# Load the women dataset
data("women")
# a. Display all rows of the dataset having height greater than 120
rows_with_height_above_120 <- subset(women, height > 120)
cat("Rows with height greater than 120:\n")
print(rows_with_height_above_120)
# b. Display all rows of the dataset in ascending order of weight
sorted_by_weight <- women[order(women$weight), ]
cat("\nRows in ascending order of weight:\n")
print(sorted_by_weight)
2. Using the inbuilt mtcars dataset perform the following.
a Display all the cars having 4 gears
b. Display all the cars having 3 and 2 carburettors
Solution:
# Load the mtcars dataset
data("mtcars")
# a. Display all the cars having 4 gears
cars_with_4_gears <- subset(mtcars, gear == 4)
cat("Cars having 4 gears:\n")
print(cars_with_4_gears)
# b. Display all the cars having 3 and 2 carburetors
cars_with_3_and_2_carb <- subset(mtcars, carb %in% c(2, 3))
cat("\nCars having 3 and 2 carburetors:\n")
print(cars_with_3_and_2_carb)
3. Using inbuilt Plant Growth dataset perform. the following.
a. Find the flowers of each type of group.
b. Display all rows of type city" having weight greater than 50
Solution:
# Load the PlantGrowth dataset
data("PlantGrowth")
# a. Find the count of flowers for each type of group
group_count <- table(PlantGrowth$group)
cat("Count of flowers for each type of group:\n")
print(group_count)
# b. Display all rows of type 'cty' having weight greater than 50
# First, we need to check the dataset as it doesn't have a 'cty' group.
# The groups available are "ctrl", "trt1", and "trt2".
# If you meant to filter by a specific group, here we will use 'trt1' as an example.
# Displaying all rows of type 'trt1' having weight greater than 50
# Since the max weight in PlantGrowth dataset is less than 50, we'll display a logical
condition.
filtered_rows <- subset(PlantGrowth, group == "trt1" & weight > 50)
cat("\nRows of type 'trt1' having weight greater than 50:\n")
print(filtered_rows)
SET A
1. Using inbuilt mtcars dataset perform the following.
a. Display all the cars having mpg mare than 20
b. Subset the dataset by mpg column for values greater than 15.0
Solution:
# Load the mtcars dataset
data("mtcars")
# a. Display all the cars having mpg more than 20
cars_high_mpg <- subset(mtcars, mpg > 20)
cat("Cars having mpg greater than 20:\n")
print(cars_high_mpg)
# b. Subset the dataset by mpg column for values greater than 15.0
subset_mtcars <- subset(mtcars, mpg > 15.0)
cat("\nSubset of the dataset for mpg values greater than 15.0:\n")
print(subset_mtcars)
2. Using the inbuilt air quality dataset perform the following
a. Find the temp. of day 30 of month 8.
b. Display the details of all days is the temp is greater than 90.
Solution:
# Load the airquality dataset
data("airquality")
# a. Find the temperature of day 30 of month 8
temp_day_30_august <- airquality[airquality$Month == 8 & airquality$Day == 30, "Temp"]
cat("Temperature of day 30 of month 8:\n")
if(length(temp_day_30_august) > 0) {
print(temp_day_30_august)
} else {
cat("No data available for day 30 of month 8.\n")
}
# b. Display details of all days where the temperature is greater than 90
high_temp_days <- subset(airquality, Temp > 90)
cat("\nDetails of all days with temperature greater than 90:\n")
print(high_temp_days)
3. Using the inbuilt airquality dataset perform the following
a. Subset the "dataset for the month July having Wind value greater than 10.
b. Find the number of days having temperature less than 60.
Solution:
# Load the airquality dataset
data("airquality")
# a. Subset the dataset for the month of July (Month = 7) having Wind value greater than 10
july_subset <- subset(airquality, Month == 7 & Wind > 10)
cat("Subset of the dataset for July with Wind greater than 10:\n")
print(july_subset)
# b. Find the number of days having temperature less than 60
days_below_60 <- sum(airquality$Temp < 60)
cat("\nNumber of days having temperature less than 60:\n")
print(days_below_60)
SET B
1. Using iris inbuilt dataset perform the following
a. find the flowers of each type of Species.
b. Find the sepal length and width of flower type setosa having
maximum petal length
Solution:
# Load the iris dataset
data("iris")
# a. Find the count of flowers of each type of Species
cat("Count of flowers for each species:\n")
species_count <- table(iris$Species)
print(species_count)
# b. Find the sepal length and width of flower type setosa having maximum petal length
cat("\nSepal length and width of 'setosa' flower having maximum petal length:\n")
setosa_flowers <- iris[iris$Species == "setosa", ]
max_petal_length <- max(setosa_flowers$Petal.Length)
setosa_max <- setosa_flowers[setosa_flowers$Petal.Length == max_petal_length, ]
sepal_length_width <- setosa_max[, c("Sepal.Length", "Sepal.Width")]
print(sepal_length_width)
b. Using iris inbuilt dataset perform the following
a. Display details of all flowers of type virginica in ascending order of petal length. (use
order function)
b. Display details of first five flowers. of type setosa having maximum petal length
Solution:
# Load the iris dataset
data("iris")
# a. Display details of all flowers of type virginica in ascending order of petal length
cat("Details of all flowers of type 'virginica' in ascending order of petal length:\n")
virginica_flowers <- iris[iris$Species == "virginica", ]
virginica_sorted <- virginica_flowers[order(virginica_flowers$Petal.Length), ]
print(virginica_sorted)
# b. Display details of first five flowers of type setosa having maximum petal length
cat("\nDetails of first five flowers of type 'setosa' having maximum petal length:\n")
setosa_flowers <- iris[iris$Species == "setosa", ]
max_petal_length <- max(setosa_flowers$Petal.Length)
setosa_max <- setosa_flowers[setosa_flowers$Petal.Length == max_petal_length, ]
first_five_setosa_max <- head(setosa_max, 5)
print(first_five_setosa_max)
c. Using inbuilt Plant Growth dataset perform the following
a Display details of all plants having weight greater than 5.80
b. Display details of all Plants of group tret1 in ascending order of their weight
Solution:
# Load the PlantGrowth dataset
data("PlantGrowth")
# a. Display details of all plants having weight greater than 5.80
cat("Details of all plants having weight greater than 5.80:\n")
plants_above_5_80 <- PlantGrowth[PlantGrowth$weight > 5.80, ]
print(plants_above_5_80)
# b. Display details of all plants of group trt1 in ascending order of their weight
cat("\nDetails of all plants of group trt1 in ascending order of their weight:\n")
trt1_plants <- PlantGrowth[PlantGrowth$group == "trt1", ]
trt1_sorted <- trt1_plants[order(trt1_plants$weight), ]
print(trt1_sorted)
SET C
1. Using inbuilt Tooth Growth dataset. perform the following.
a. Find supplements (supp) wise maximum Cand minimum length of tooth.
b. Display details of first 3 tooth having minimum length for supplement
OJ for dose 1.0.
Solution:
# Load the ToothGrowth dataset
data("ToothGrowth")
# a. Find supplements (supp) wise maximum and minimum length of tooth
library(dplyr)
# Summary statistics for each supplement
supp_summary <- ToothGrowth %>%
group_by(supp) %>%
summarise(
Max_Length = max(len),
Min_Length = min(len)
)
# Display the summary
cat("Maximum and Minimum Tooth Length by Supplement:\n")
print(supp_summary)
# b. Display details of first 3 teeth having minimum length for supplement OJ for dose 1.0
min_length_OJ_dose_1 <- ToothGrowth %>%
filter(supp == "OJ", dose == 1.0) %>%
arrange(len) %>%
slice_head(n = 3)
# Display the details
cat("\nDetails of first 3 teeth with minimum length for Supplement OJ and Dose 1.0:\n")
print(min_length_OJ_dose_1)