Module # 8 Input/Output, string manipulation and plyr package
The file that was imported for this assignment is titled "Assignment 6 Dataset.txt"
Before I began any coding I downloaded install.packages("plyr") and install.packages("pryr") then ran these libraries:
require(pryr)
require(ISLR)
require(boot)
library(data.table)
library(plyr)
For step 1 I imported the dataset by using read.table, which resulted in the code below:
> #Step 1
> Student_assignment_6 <- read.table("C:/Users/alenm/OneDrive/Documents/LIS4370/Assignment 6 Dataset.txt", header = TRUE, sep = ",")
> 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
7 Raphael 23 Male 91
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
14 Fallon 22 Female 90
15 Deloris 21 Female 67
16 Randee 23 Female 91
17 Eboni 20 Female 84
18 Delfina 19 Female 93
19 Ernestina 19 Female 93
20 Milo 19 Male 67The next step was to calculate :
sex = Student$Sex
mean(Sex)
However this could not be performed due to the value not being Numerical. I did fine a solution to
this by tweaking the code. Below is the result.
This also be calculated by using plyr and ddply as.
> StudentAverage = ddply(Student_assignment_6,"Sex",transform, Grade.Average=mean(Grade))
> StudentAverage
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.2500Step 2 requires the output to be turned into a file. This can be done by using write.table
We get this:
"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.25
"18" "Booker" 18 "Male" 83 80.25
"19" "Raphael" 23 "Male" 91 80.25
"20" "Milo" 19 "Male" 67 80.25
To make this into a CSV file we use sep"," as shown below:
> write.table(StudentAverage,"Sorted_Average",sep=",")"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.25
"18","Booker",18,"Male",83,80.25
"19","Raphael",23,"Male",91,80.25
"20","Milo",19,"Male",67,80.25
Next we're going to filter the original data set to include only data for which the student name
contains the letter i.
> i_students <- subset(Student_assignment_6, grepl("i", Student_assignment_6$Name, ignore.case=T))
> 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
We're then going to turn this subset into a CSV file.
> write.table(i_students,"DataSubset",sep=",")"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
Overview:
This assignment was pretty interesting. I did get stuck at a few spots but now I understand how to create CSV files and what exactly consists of it. I always thought it was a complicated and time consuming process but I now know to incorporate these functions and packages in R to provide these files. Overall this was a great practice and I'm excited to add this under my R skills!
or
https://github.com/AlenjaksAbraham/LIS4370.git
To see the code look under Module 8.R.
You can also see the various text files that were produced under this assignment.
Comments
Post a Comment