Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

10. Write a R program for with any dataset containing data frame objects, and employ manipulating and analyzing data. || R

 # Load the mtcars dataset (if not already loaded) 

data(mtcars) 

# Display the first few rows of the dataset 

head(mtcars) 

# Calculate the mean and median of miles per gallon 

(mpg) 

mean_mpg <- mean(mtcars$mpg) 

median_mpg <- median(mtcars$mpg)

# Display the mean and median 

cat("Mean MPG:", mean_mpg, "\n") 

cat("Median MPG:", median_mpg, "\n") 

# Find the car with the highest miles per gallon (mpg) 

max_mpg_car <- mtcars[mtcars$mpg == max(mtcars$mpg), ] 

# Display the car with the highest MPG 

cat("Car with the highest MPG:\n") 

print(max_mpg_car) 

# Filter cars with more than 100 horsepower 

high_hp_cars <- mtcars[mtcars$hp > 100, ] 

# Display cars with more than 100 horsepower 

cat("Cars with more than 100 HP:\n") 

print(high_hp_cars)





OR IN A SIMPLE WAY


# Create a simple custom dataset (data frame) 

data <- data.frame( 

StudentID = 1:10, 

Age = c(22, 21, 23, 20, 22, 24, 19, 20, 21, 22), 

Score = c(85, 92, 78, 65, 89, 73, 97, 81, 88, 90) 

# Display the dataset 

print(data) 

# Calculate the mean and median of the 'Score' column 

mean_score <- mean(data$Score) 

median_score <- median(data$Score) 

cat("Mean Score:", mean_score, "\n") 

cat("Median Score:", median_score, "\n") 

# Filter students older than 21 

older_students <- data[data$Age > 21, ] 

cat("Students older than 21:\n") 

print(older_students) 



Post a Comment

0 Comments