R Programming Lab Test Answers and Solutions for Beginners
The R Programming Lab Test Answers page provides verified solutions for every R lab question. Each example includes RStudio code for dataframes, CSV imports, and mean calculations using the aggregate() function.
Step-by-Step R Solutions Explained
Students can explore how to create, modify, and export dataframes in R. The R Programming Lab Test for Beginners solutions focus on understanding how data analysis works through real datasets. Moreover, these answers improve problem-solving and coding logic effectively.
Enhance Your R Programming Practice
Additionally, each R lab solution includes comments and explanations, making it easy to learn syntax and functions like data.frame(), read.csv(), and write.csv(). Therefore, these solutions are ideal for beginners who want practical experience with R data operations.
R Programming Lab — Answer Key
This page shows the correct R code and the expected output for each question.
Q1. Create the dataframe in RStudio
student_id <- c("S01","S02","S03","S04","S05","S06","S07","S08","S09","S10","S11","S12","S13","S14","S15")
Math <- c(85,60,88,47,59,90,73,93,66,93,69,83,79,80,90)
Science <- c(47,54,87,41,72,81,77,91,57,61,46,56,47,58,44)
English <- c(38,52,46,46,38,88,70,55,54,48,80,40,83,81,40)
Social_Science <- c(65,43,84,57,55,58,30,84,57,41,74,88,40,54,59)
student_marks <- data.frame(
StudentID = student_id,
Math = Math,
Science = Science,
English = English,
Social_Science = Social_Science
)
Q2. Export dataframe as CSV
write.csv(student_marks, "student_marks.csv", row.names = FALSE)
Q3. Import CSV into RStudio
marks <- read.csv("student_marks.csv", stringsAsFactors = FALSE)
head(marks)
Q4. Mean of each numerical column
mean_math <- mean(marks$Math)
mean_science <- mean(marks$Science)
mean_english <- mean(marks$English)
mean_social <- mean(marks$Social_Science)
Math = 77.00
Science = 61.27
English = 57.27
Social_Science = 59.27
Q5. Create dataframe mean_data
mean_data <- data.frame(
Math = round(mean_math, 2),
Science = round(mean_science, 2),
English = round(mean_english, 2),
Social_Science = round(mean_social, 2)
)
mean_data
Math = 77.00
Science = 61.27
English = 57.27
Social_Science = 59.27
Q6. Using aggregate() to compute means
aggregate(. ~ 1, data = marks[, c("Math","Science","English","Social_Science")], FUN = mean)
Math = 77.00000
Science = 61.26667
English = 57.26667
Social_Science = 59.26667
