Data Analysis and Visualization of Student Marks from a CSV File and Writing Results into a CSV file Using R
Data Analysis and Visualization of Student Marks from a CSV File and Writing Results into a CSV file Using R
Aim
To read a CSV file containing student records and perform data analysis and visualization using R.
Objective
- To import data from a CSV file into R.
- To explore the structure and summary statistics of the data.
- To compute total marks and average marks.
- To identify toppers and passed students.
- To visualize the data using various plots.
Theory
A CSV (Comma Separated Values) file is a simple text file used to store tabular data. R provides the read.csv() function to read CSV files into a data frame and write.csv() function to write data into a csv file.
Data analysis involves:
- Exploring the data.
- Computing statistical measures.
- Filtering and sorting records.
- Summarizing the dataset.
Data visualization helps in understanding patterns and distributions through graphical representations such as:
- Bar plots
- Histograms
- Box plots
- Pie charts
- Scatter plots
Algorithm
- Create a CSV file named stud.csv.
- Read the CSV file into a data frame.
- Display the dataset and its structure.
- Compute total marks and average marks.
- Find the topper.
- Find students who passed all subjects.
- Display summary statistics.
- Write students list in the descending order of total marks to a csv file.
- Visualize the data using bar plots, histograms, box plots, pie charts, and scatter plots.
Sample CSV File (stud.csv)
Rno,Name,M1,M2,M3
101,John,85,78,92
102,Mary,95,88,90
103,Alex,45,65,70
104,David,76,82,80
105,Sara,90,95,98
106,Tom,55,60,50
107,Riya,88,91,84
108,Sam,62,70,68
Save this file in the current working directory.
Program
# Read the CSV file
stud <- read.csv("stud.csv")
# Display data
print(stud)
# Structure
str(stud)
# Summary statistics
summary(stud)
# Dimensions
dim(stud)
# Add total and average columns
stud$Total <- stud$M1 + stud$M2 + stud$M3
stud$Average <- stud$Total/3
# Display updated data
print(stud)
# Topper
topper <- stud[stud$Total == max(stud$Total), ]
cat("Topper:\n")
print(topper)
# Students passed in all subjects
passed <- stud[stud$M1>=50 &
stud$M2>=50 &
stud$M3>=50, ]
cat("Passed Students:\n")
print(passed)
# Students scoring above 90 average
high <- stud[stud$Average>90, ]
cat("Students with Average > 90:\n")
print(high)
# Sort by total marks
sorted <- stud[order(stud$Total,
decreasing=TRUE), ]
cat("Sorted by Total Marks:\n")
print(sorted)
# Write to CSV file write.csv(sorted, "sorted_students.csv", row.names=FALSE) cat("Sorted student list saved to sorted_students.csv")
Outputs
> stud <- read.csv("stud.csv")
> # Display data
> print(stud)
Rno Name M1 M2 M3
1 101 John 85 78 92
2 102 Mary 95 88 90
3 103 Alex 45 65 70
4 104 David 76 82 80
5 105 Sara 90 95 98
6 106 Tom 55 60 50
7 107 Riya 88 91 84
8 108 Sam 62 70 68
> # Structure
> str(stud)
'data.frame': 8 obs. of 5 variables:
$ Rno : int 101 102 103 104 105 106 107 108
$ Name: chr "John" "Mary" "Alex" "David" ...
$ M1 : int 85 95 45 76 90 55 88 62
$ M2 : int 78 88 65 82 95 60 91 70
$ M3 : int 92 90 70 80 98 50 84 68
> # Summary statistics
> summary(stud)
Rno Name M1 M2 M3
Min. :101.0 Length :8 Min. :45.00 Min. :60.00 Min. :50.0
1st Qu.:102.8 N.unique :8 1st Qu.:60.25 1st Qu.:68.75 1st Qu.:69.5
Median :104.5 N.blank :0 Median :80.50 Median :80.00 Median :82.0
Mean :104.5 Min.nchar:3 Mean :74.50 Mean :78.62 Mean :79.0
3rd Qu.:106.2 Max.nchar:5 3rd Qu.:88.50 3rd Qu.:88.75 3rd Qu.:90.5
Max. :108.0 Max. :95.00 Max. :95.00 Max. :98.0
> # Dimensions
> dim(stud)
[1] 8 5
> # Add total and average columns
> stud$Total <- stud$M1 + stud$M2 + stud$M3
> stud$Average <- stud$Total/3
> # Display updated data
> print(stud)
Rno Name M1 M2 M3 Total Average
1 101 John 85 78 92 255 85.00000
2 102 Mary 95 88 90 273 91.00000
3 103 Alex 45 65 70 180 60.00000
4 104 David 76 82 80 238 79.33333
5 105 Sara 90 95 98 283 94.33333
6 106 Tom 55 60 50 165 55.00000
7 107 Riya 88 91 84 263 87.66667
8 108 Sam 62 70 68 200 66.66667
> # Topper
> topper <- stud[stud$Total == max(stud$Total), ]
> cat("Topper:\n")
Topper:
> print(topper)
Rno Name M1 M2 M3 Total Average
5 105 Sara 90 95 98 283 94.33333
> # Students passed in all subjects
> passed <- stud[stud$M1>=50 &
+ stud$M2>=50 &
+ stud$M3>=50, ]
> cat("Passed Students:\n")
Passed Students:
> print(passed)
Rno Name M1 M2 M3 Total Average
1 101 John 85 78 92 255 85.00000
2 102 Mary 95 88 90 273 91.00000
4 104 David 76 82 80 238 79.33333
5 105 Sara 90 95 98 283 94.33333
6 106 Tom 55 60 50 165 55.00000
7 107 Riya 88 91 84 263 87.66667
8 108 Sam 62 70 68 200 66.66667
> # Students scoring above 90 average
> high <- stud[stud$Average>90, ]
> cat("Students with Average > 90:\n")
Students with Average > 90:
> print(high)
Rno Name M1 M2 M3 Total Average
2 102 Mary 95 88 90 273 91.00000
5 105 Sara 90 95 98 283 94.33333
> # Sort by total marks
> sorted <- stud[order(stud$Total,
+ decreasing=TRUE), ]
> cat("Sorted by Total Marks:\n")
Sorted by Total Marks:
> print(sorted)
Rno Name M1 M2 M3 Total Average
5 105 Sara 90 95 98 283 94.33333
2 102 Mary 95 88 90 273 91.00000
7 107 Riya 88 91 84 263 87.66667
1 101 John 85 78 92 255 85.00000
4 104 David 76 82 80 238 79.33333
8 108 Sam 62 70 68 200 66.66667
3 103 Alex 45 65 70 180 60.00000
6 106 Tom 55 60 50 165 55.00000
> cat("Sorted student list saved to sorted_students.csv")
Sorted student list saved to sorted_students.csv
Visualization
1. Bar Plot of Total Marks
2. Histogram of Average Marks
3. Box Plot of Subject Marks
4. Scatter Plot of M1 vs M2
5. Pie Chart of Students with Average ≥ 75 and < 75
6. Multiple Graphs Together
par(mfrow=c(2,2))
barplot(stud$Total,
names.arg=stud$Name,
col="skyblue",
main="Total Marks")
hist(stud$Average,
col="yellow",
main="Average Marks")
boxplot(stud$M1,
stud$M2,
stud$M3,
names=c("M1","M2","M3"),
col=c("red","green","blue"),
main="Subject Marks")
plot(stud$M1,
stud$M2,
pch=19,
col="blue",
main="M1 vs M2")
Result
The student data were successfully imported from the CSV file using the read.csv() function. Statistical analysis was performed by calculating total marks and average marks, identifying toppers, and filtering passed students. Various graphical techniques such as bar plots, histograms, box plots, pie charts, and scatter plots were used to visualize the data effectively.
Comments
Post a Comment