library(dplyr)

Exercise 1: Filtering a DataFrame

Using the mtcars dataset, create a new DataFrame that only includes rows where the number of cylinders is greater than 4 and the horsepower is greater than 150.

# Load the mtcars dataset
data(mtcars)

# Filter the DataFrame to get only the records where the number of cylinders is greater than 4 and the horsepower is greater than 150
filtered_mtcars <- mtcars[mtcars$cyl > 4 & mtcars$hp > 150, ]

# Print the result
print(filtered_mtcars)
library(dplyr)
# Load the mtcars dataset
data(mtcars)

# Filter the DataFrame
filtered_mtcars <- mtcars %>% 
  filter(cyl > 4, hp > 150)

# Print the result
print(filtered_mtcars)

Exercise 2: Grouping and Summarizing

Using the iris dataset, group the data by species and calculate the mean sepal length for each species.

# Load the iris dataset
data(iris)

# Group the DataFrame by species and calculate the mean sepal length for each species
iris_summary <- aggregate(iris$Sepal.Length, by = list(iris$Species), mean)

# Print the result
print(iris_summary)
library(dplyr)

# Load the iris dataset
data(iris)

# Group the data and calculate mean sepal length
iris_summary <- iris %>% 
  group_by(Species) %>% 
  summarise(mean_sepal_length = mean(Sepal.Length))

# Print the result
print(iris_summary)

Exercise 3: Creating a Bar Plot

Using the ggplot2 package, create a bar plot of the number of cylinders in the mtcars dataset.

# Load the ggplot2 package
library(ggplot2)

# Create a bar plot
ggplot(mtcars, aes(x = factor(cyl))) + 
  geom_bar()

Exercise 4: Merging DataFrames

Using the dplyr package, merge the mtcars dataset with a new DataFrame that contains the country of origin for each car model.

# Load the dplyr package
library(dplyr)

# Create a new column with car models
mtcars$model <- rownames(mtcars)

# Create a new DataFrame with country of origin
country_df <- data.frame(model = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout"),
                         country = c("Japan", "Japan", "Japan", "USA", "USA"))

# Merge the DataFrames
merged_df <- left_join(mtcars, country_df, by = "model")

# Print the result
print(merged_df)

Exercise 5: Creating a Scatter Plot

Using the ggplot2 package, create a scatter plot of the relationship between horsepower and miles per gallon in the mtcars dataset.

plot(mtcars$hp, mtcars$mpg)
# Create a scat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