Module 8 Input/ Output, String Manipulation and pylr package
First, the required package plyr is installed and loaded. This package allows grouped operations on datasets. The dataset is then imported using read.table() with file.choose(), which opens a prompt to select the file from the computer. Next, the ddply() function groups the dataset by the Sex variable and calculates the mean of the Grade column for each group. A new variable called Grade.Average is created containing the calculated means. Finally, the results are written to a text file using write.table() so they can be saved outside of R.
The line y <- ddply(x, "Sex", transform, Grade.Average = mean(Grade)) calculates the average grade grouped by the Sex category. The ddply() function separates the dataset by the values in the Sex column and calculates the mean of the Grade column for each group. The result is stored in a new variable called y, and a new column called Grade.Average is added to display the calculated mean values. The command write.table(y, "Sorted_Average.csv", sep=",") exports the results of the grouped mean calculation to a file. The dataset y is written to a file named Sorted_Average.csv. The sep="," argument ensures that the file is written as a comma-separated values file, which can easily be opened in spreadsheet programs such as Excel. The line newx <- subset(x, grepl("[iI]", x$Name)) filters the dataset to identify rows where the Name column contains the letter i. The subset() function creates a smaller dataset based on a condition. The grepl() function searches text for a pattern, and the pattern [iI] tells R to match both lowercase i and uppercase I. The resulting filtered dataset is stored in the variable newx. The final command write.table(newx, "DataSubset.csv", sep=",") exports the filtered dataset containing names with the letter i to a file named DataSubset.csv. The sep="," argument again ensures that the file is written as a comma-separated values file so it can be easily opened and used in other programs.
Here are the charts that the code showed.

.png)
.png)
.png)
Comments
Post a Comment