0% found this document useful (0 votes)
16 views2 pages

R Statistics Assignment: Data Analysis Tasks

This assignment for the course 'Statistics with R' includes various tasks related to data manipulation and visualization using R. Students are required to work with built-in datasets like mtcars and iris, perform operations such as exporting, importing, filtering, and plotting data, and apply both base R and tidyverse tools. The deadline for submission is November 8, 2025, and the total marks for the assignment are 15.

Uploaded by

Arushi Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

R Statistics Assignment: Data Analysis Tasks

This assignment for the course 'Statistics with R' includes various tasks related to data manipulation and visualization using R. Students are required to work with built-in datasets like mtcars and iris, perform operations such as exporting, importing, filtering, and plotting data, and apply both base R and tidyverse tools. The deadline for submission is November 8, 2025, and the total marks for the assignment are 15.

Uploaded by

Arushi Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment

SEC 1: Statistics with R


Deadline : 8 November 2025 Total Marks: 15

Instructions
 Use clear and well-commented R code.
 Where applicable, show your code, output, and interpretation.
 For data manipulation and visualization, use both base R and tidyverse tools
(especially dplyr and ggplot2)

Q1. Export the built-in mtcars dataset to a .csv file in your working directory. Then, import it
back into R as a new object called cars_data.

Q2. Import the dataset available at the URL below and display the first 10 rows:
Link: [Link]

Q3. Export the built-in iris dataset to an Excel .xlsx file using a suitable R package (writexl or
openxlsx). Save it with the name iris_export.xlsx.

Q4. Using the dplyr package, calculate the average horsepower (hp) and weight (wt) grouped
by the number of cylinders (cyl) in the mtcars dataset.

Q5. Using base R, filter the mtcars dataset to include only cars with:

 Miles per gallon (mpg) greater than 20


 Horsepower (hp) less than 100
Display the first 5 rows of the resulting dataset.

Q6. Create a new column called efficiency in the mtcars dataset, defined as:
efficiency = mpg / wt
Do this using:

 (a) Base R
 (b) mutate() from dplyr

Q7. Sort the mtcars dataset in descending order of mpg. Do this using:

 (a) Base R
 (b) arrange() from dplyr
Q8. Using base R, create a histogram of the mpg variable in the mtcars dataset.
Include appropriate:

 Axis labels
 Plot title

Q9. Using ggplot2, create a boxplot showing mpg across different numbers of cylinders (cyl) in
the mtcars dataset.

Q10. Create a scatter plot using ggplot2 that shows the relationship between wt (x-axis) and
mpg (y-axis).

 Color the points by cyl


 Add a linear regression line

Q11. Use base R to create a pairwise scatterplot matrix for the following variables in
mtcars: mpg, hp, wt, and qsec.

Q12. Fit a linear regression model to predict mpg using the variables wt and hp in the mtcars
dataset.
Display the model summary.

Q13. Based on the model summary:

 Interpret the coefficients.


 Comment on the values of R-squared and Adjusted R-squared.
 What do these values tell you about the model's performance?

Q14. Using ggplot2, plot mpg vs wt and add the fitted regression line from the model.
Use geom_point() and geom_smooth(method = "lm").

Q15. Using the model created in Q12, predict the mpg for the first 5 cars in the mtcars dataset.
Compare the predicted values with the actual mpg.

Common questions

Powered by AI

A linear regression model helps predict mpg based on wt and hp. The coefficients indicate the model's estimate of mpg changes: a negative coefficient for wt (e.g., -3.917) suggests higher weight decreases mpg, whereas a positive hp coefficient would imply the opposite. The R-squared and Adjusted R-squared values measure fit quality; high values (close to 1) indicate strong predictivity, assessing the model's explanatory power and efficiency for the Mtcar's dataset .

With base R, filter the dataset using subset() for cars having 'mpg' greater than 20 and 'hp' less than 100 like this: subset(mtcars, mpg > 20 & hp < 100). Displaying the first five rows of this filtered data shows the specific cars meeting these criteria, illustrating basic conditional operations in R .

To export the 'mtcars' dataset to a .csv file, you can use the write.csv() function with the dataset and specify the file path. For example: write.csv(mtcars, 'mtcars_export.csv'). To read it back into R as a new object called cars_data, you use the read.csv() function: cars_data <- read.csv('mtcars_export.csv').

Using the dplyr package, you can calculate these averages by grouping the dataset by the 'cyl' column and then summarizing the mean values. Specifically, you would use: mtcars %>% group_by(cyl) %>% summarize(avg_hp = mean(hp), avg_wt = mean(wt)).

Predict new mpg values using the predict() function with the model object and new data: predict(lm_model, newdata=mtcars[1:5,]). This compares predicted mpg against actual values for the first five cars. Differences illustrate the model's precision and areas where it diverges, providing insights into model tracking and potential improvements .

Using ggplot2, a boxplot to explore 'mpg' across 'cyl' can be created with ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot(). This displays variations in mpg distribution for each cylinder category, facilitating an understanding of how cylinder numbers impact fuel efficiency .

Pairwise scatterplot matrices, created with pairs(mtcars[,c('mpg', 'hp', 'wt', 'qsec')]), allow visualization of potential relationships between multiple variables simultaneously. They help identify correlations, outliers, and patterns within the data, offering deeper insights in exploratory analysis of the 'mtcars' dataset .

To add an 'efficiency' column defined as mpg / wt, use either base R or dplyr. In base R, achieve this with mtcars$efficiency <- mtcars$mpg / mtcars$wt. Alternatively, with dplyr's mutate(), use: mtcars <- mtcars %>% mutate(efficiency = mpg / wt). Both methods update the dataset with a column illustrating fuel efficiency relative to weight .

In base R, you can sort the 'mtcars' dataset by 'mpg' in descending order using the order() function: mtcars <- mtcars[order(-mtcars$mpg),]. With dplyr, use arrange(mtcars, desc(mpg)) for a more readable syntax. Both reorder the dataset to highlight cars with the highest mpg values first .

Base R provides fundamental functions for data manipulation and visualization, such as subsetting data and basic plotting. Tidyverse, including tools like dplyr and ggplot2, offers more readable syntax and powerful operations for both tasks. Integrating both helps achieve efficiency and enhances clarity and functionality of R scripts by combining the simplicity of base R with the complexity and aesthetics of tidyverse .

You might also like