Module # 8 Input/Output, string manipulation and plyr package
Input/Output, string manipulation and plyr package
Part 1
First, importing the dataset into R using read.table() and specified that the file is comma-separated.
Input:
students <- read.table(file.choose(), header = TRUE, sep = ",")
students
Output:
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 67Next, using the plyr package and the ddply() function to calculate the average grade grouped.
Input:
students_gendered_mean <- ddply(students, "Sex", summarise,
Grade_Average = mean(Grade))
students_gendered_mean
Output:
Sex Grade_Average
1 Female 86.9375
2 Male 80.2500
Finally, I exported the results to a file.
write.table(students_gendered_mean, "Students_Gendered_Mean.txt", row.names = FALSE)
Part 2
Filtering the dataset using subset() and grepl() to select students whose names contain the letter i.
Input:
i_students <- subset(students, grepl("i", students$Name, ignore.case = TRUE)) i_students
Output:
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
The filtered dataset was written to a CSV file.
write.csv(i_students,
"Students_with_i.csv",
row.names = FALSE)
Comments
Post a Comment