Module 8
This assignment shows how to load data, calculate averages by groups, find specific patterns in text, and save results to files.
Part 1: Load the Data
library(plyr)
> student6 = read.table(file.choose(), header = TRUE, stringsAsFactors = FALSE)
> str(student6)
'data.frame': 20 obs. of 1 variable:
$ Name.Age.Sex.Grade: chr "Raul,25,Male,80" "Booker,18,Male,83" "Lauri,21,Female,90" "Leonie,21,Female,91" ...
> head(student6)
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
What it does: Loads the student data file into R and checks that it imported correctly with 20 students and their information.
Part 2: Calculate Average Grades
#Step 2
> student6 = read.table(file.choose(), header = TRUE, stringsAsFactors = FALSE, sep = ",")
> str(student6)
'data.frame': 20 obs. of 4 variables:
$ Name : chr "Raul" "Booker" "Lauri" "Leonie" ...
$ Age : int 25 18 21 21 22 20 23 24 21 23 ...
$ Sex : chr "Male" "Male" "Female" "Female" ...
$ Grade: int 80 83 90 91 85 69 91 97 78 81 ...
> head(student6)
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
> gender_mean = ddply(student6, "Sex", summarise, GradeAverage = mean(Grade, na.rm = TRUE))
> gender_mean
Sex GradeAverage
1 Female 86.9375
2 Male 80.2500
What it does: Calculates the average grade separately for males and females using the plyr package.
Part 3: Save Averages to File
#stp 3
> write.table(gender_mean, file = "gender_mean.txt", sep = "\t", row.names = FALSE)
> list.files(pattern = "gender_mean.txt")
[1] "gender_mean.txt"
What it does: Saves the average grades to a text file so you can open it later or share it with others.
Part 4: Find Names with Letter i
#step 4
> i_students = subset(student6, grepl("[iI]", student6$Name))
> 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
What it does: Finds all students who have the letter i anywhere in their name, whether uppercase or lowercase.
Part 5: Save Just the Names
> #step 5
> write.csv(i_students$Name, file = "i_students.csv", row.names = FALSE, quote = FALSE)
What it does: Creates a CSV file with just the names of students who have i in their name.
Part 6: Save Complete Filtered Data
#Step 6
> write.csv(i_students, file = "i_students_full.csv", row.names = FALSE)
>
>
>
>
>
> View(student6)
> View(i_students)
> View(student6)
> View(i_students)
> View(gender_mean)
> list.files(pattern = "*.txt|*.csv")
[1] "gender_mean.txt" "i_students_full.csv" "i_students.csv"
What it does: Saves all the information about students with i in their names and confirms all three files were created successfully.


Comments
Post a Comment