Module # 8 Input/Output, string manipulation and plyr package
> # Load package
> library(plyr)
>
> # Step 1: Import the dataset
> students <- read.table("Assignment 6 Dataset.txt", header = TRUE, sep = ",")
>
> # Mean Grade using Sex as the category
> students_gendered_mean <- ddply(students, "Sex", transform,
+ Grade.Average = mean(Grade))
>
> # View result
> students_gendered_mean
Name Age Sex Grade Grade.Average
1 Lauri 21 Female 90 86.9375
2 Leonie 21 Female 91 86.9375
3 Sherlyn 22 Female 85 86.9375
4 Mikaela 20 Female 69 86.9375
5 Aiko 24 Female 97 86.9375
6 Tiffaney 21 Female 78 86.9375
7 Corina 23 Female 81 86.9375
8 Petronila 23 Female 98 86.9375
9 Alecia 20 Female 87 86.9375
10 Shemika 23 Female 97 86.9375
11 Fallon 22 Female 90 86.9375
12 Deloris 21 Female 67 86.9375
13 Randee 23 Female 91 86.9375
14 Eboni 20 Female 84 86.9375
15 Delfina 19 Female 93 86.9375
16 Ernestina 19 Female 93 86.9375
17 Raul 25 Male 80 80.2500
18 Booker 18 Male 83 80.2500
19 Raphael 23 Male 91 80.2500
20 Milo 19 Male 67 80.2500
>
> # Write output to a file
> write.table(students_gendered_mean, "Students_Gendered_Mean.csv",
+ sep = ",", row.names = FALSE)
>
> # Step 2: Create a new dataset with names containing i
> i_students <- subset(students, grepl("i", Name, ignore.case = TRUE))
>
> # View filtered data
> i_students
Name Age Sex Grade
3 Lauri 21 Female 90
4 Leonie 21 Female 91
6 Mikaela 20 Female 69
8 Aiko 24 Female 97
9 Tiffaney 21 Female 78
10 Corina 23 Female 81
11 Petronila 23 Female 98
12 Alecia 20 Female 87
13 Shemika 23 Female 97
15 Deloris 21 Female 67
17 Eboni 20 Female 84
18 Delfina 19 Female 93
19 Ernestina 19 Female 93
20 Milo 19 Male 67
>
> # Step 3: Write filtered dataset to CSV
> write.csv(i_students, "i_students.csv", row.names = FALSE)For this assignment, I imported the Assignment 6 dataset into R and used the plyr package to analyze the data. Using the ddply() function, I calculated the mean grade for students grouped by sex. The results showed that the average grade for female students was 86.94, while the average grade for male students was 80.25. After calculating the averages, I created a new dataset by filtering the original data to include only student names that contained the letter “i” (case insensitive). This subset of the data was then written to a CSV file for further use. These steps demonstrate how R can be used to import datasets, perform grouped statistical analysis, filter observations, and export results to external files.
Comments
Post a Comment