Module # 8 Input/Output, string manipulation and plyr package

Issaiah Jennings

Module # 8 Input/Output, string manipulation and plyr package


For this assignment, I worked with a dataset containing student information such as their name, age, sex, and grade. The first thing I did was import the data from a .txt file on my Desktop into R using the read.delim() function. Once the data was in R, I renamed the columns for clarity so they were easier to work with. After that, I used the plyr package to calculate the average grade based on gender by grouping the data by the "Sex" column and applying the mean() function. Next, I filtered the data to only include students whose names contained the letter "i," using the grepl() function. Finally, I saved the results of the mean grade calculation into a text file and exported the filtered student names to a CSV file, which I saved on my Desktop.



> # Install and load required packages
> library(plyr)
>
> # Read the dataset with read.delim() assuming it's tab-separated
> Student_assignment_6 <- read.delim("/Users/zayjenings/Desktop/Assignment 6 Dataset.txt", header = TRUE)
>
> # Rename columns 
> colnames(Student_assignment_6) <- c("Name", "Age", "Sex", "Grade")
>
> # View the first few rows to confirm the changes
> head(Student_assignment_6)
Name Age Sex Grade
1 Raul 25 Male 80
2 Booker 18 Male 83
3 Lauri 21 Female 90
4 Leonie 21 Female 91
5 Sherlyn 22 Female 85
6 Mikaela 20 Female 69
>
> # Calculate the mean of the 'Grade' column based on 'Sex'
> StudentAverage <- ddply(Student_assignment_6, "Sex", transform, Grade.Average = mean(Grade))
>
> # Write the result to a file
> write.table(StudentAverage, "Students_Gendered_Mean.txt")
>
> i_students <- subset(Student_assignment_6, grepl("i", Name, ignore.case = TRUE))
>
>
> write.csv(i_students, "Filtered_Students.csv")
"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," 23 "Female" 97 86.9375
"11" "Fallon,22,Female," 22 "Female" 90 86.9375
"12" "Deloris,21,Female," 21 "Female" 67 86.9375
"13" "Randee,23,Female," 23 "Female" 91 86.9375
"14" "Eboni,20,Female," 20 "Female" 84 86.9375
"15" "Delfina,19,Female," 19 "Female" 93 86.9375
"16" "Ernestina" 19 "Female" 93 86.9375
"17" "Raul" 25 "Male" 80 80.25
"18" "Booker" 18 "Male" 83 80.25
"19" "Raphael" 23 "Male" 91 80.25
"20" "Milo" 19 "Male" 67 80.25


> write.csv(i_students, "/Users/zayjenings/Desktop/Filtered_Students.csv")




https://github.com/Ijennin/R-Functions/blob/519edc263cfdf3ef151e2af03acfe0778f5e730e/Module%20%23%208

>

Comments

Popular posts from this blog

Final project - create your own R Package

Module # 1 assignment(R)

Module # 4 Programming structure in R