This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# Load required libraries
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Load the text data
text1 <- readLines('Coursera-SwiftKey/final/en_US/en_US.blogs.txt', warn = FALSE)
text2 <- readLines('Coursera-SwiftKey/final/en_US/en_US.news.txt', warn = FALSE)
text3 <- readLines('Coursera-SwiftKey/final/en_US/en_US.twitter.txt', warn = FALSE)
# Basic summaries
summarize_text_data <- function(text_data) {
word_count <- sum(sapply(strsplit(text_data, " "), length))
line_count <- length(text_data)
return(data.frame(Word_Count = word_count, Line_Count = line_count))
}
summary1 <- summarize_text_data(text1)
summary2 <- summarize_text_data(text2)
summary3 <- summarize_text_data(text3)
# Combine summaries into a single table
summary_table <- rbind(
cbind(File = "File 1", summary1),
cbind(File = "File 2", summary2),
cbind(File = "File 3", summary3)
)
print("Summary Table:")
## [1] "Summary Table:"
print(summary_table)
## File Word_Count Line_Count
## 1 File 1 37334131 899288
## 2 File 2 2643969 77259
## 3 File 3 30373543 2360148
# Basic data tables (showing first 5 lines of each file)
basic_tables <- list(
File1 = head(text1, 5),
File2 = head(text2, 5),
File3 = head(text3, 5)
)
print("Basic Data Tables:")
## [1] "Basic Data Tables:"
print(basic_tables)
## $File1
## [1] "In the years thereafter, most of the Oil fields and platforms were named after pagan “gods”."
## [2] "We love you Mr. Brown."
## [3] "Chad has been awesome with the kids and holding down the fort while I work later than usual! The kids have been busy together playing Skylander on the XBox together, after Kyan cashed in his $$$ from his piggy bank. He wanted that game so bad and used his gift card from his birthday he has been saving and the money to get it (he never taps into that thing either, that is how we know he wanted it so bad). We made him count all of his money to make sure that he had enough! It was very cute to watch his reaction when he realized he did! He also does a very good job of letting Lola feel like she is playing too, by letting her switch out the characters! She loves it almost as much as him."
## [4] "so anyways, i am going to share some home decor inspiration that i have been storing in my folder on the puter. i have all these amazing images stored away ready to come to life when we get our home."
## [5] "With graduation season right around the corner, Nancy has whipped up a fun set to help you out with not only your graduation cards and gifts, but any occasion that brings on a change in one's life. I stamped the images in Memento Tuxedo Black and cut them out with circle Nestabilities. I embossed the kraft and red cardstock with TE's new Stars Impressions Plate, which is double sided and gives you 2 fantastic patterns. You can see how to use the Impressions Plates in this tutorial Taylor created. Just one pass through your die cut machine using the Embossing Pad Kit is all you need to do - super easy!"
##
## $File2
## [1] "He wasn't home alone, apparently."
## [2] "The St. Louis plant had to close. It would die of old age. Workers had been making cars there since the onset of mass automotive production in the 1920s."
## [3] "WSU's plans quickly became a hot topic on local online sites. Though most people applauded plans for the new biomedical center, many deplored the potential loss of the building."
## [4] "The Alaimo Group of Mount Holly was up for a contract last fall to evaluate and suggest improvements to Trenton Water Works. But campaign finance records released this week show the two employees donated a total of $4,500 to the political action committee (PAC) Partners for Progress in early June. Partners for Progress reported it gave more than $10,000 in both direct and in-kind contributions to Mayor Tony Mack in the two weeks leading up to his victory in the mayoral runoff election June 15."
## [5] "And when it's often difficult to predict a law's impact, legislators should think twice before carrying any bill. Is it absolutely necessary? Is it an issue serious enough to merit their attention? Will it definitely not make the situation worse?"
##
## $File3
## [1] "How are you? Btw thanks for the RT. You gonna be in DC anytime soon? Love to see you. Been way, way too long."
## [2] "When you meet someone special... you'll know. Your heart will beat more rapidly and you'll smile for no reason."
## [3] "they've decided its more fun if I don't."
## [4] "So Tired D; Played Lazer Tag & Ran A LOT D; Ughh Going To Sleep Like In 5 Minutes ;)"
## [5] "Words from a complete stranger! Made my birthday even better :)"
# Visualizations
# Word counts histogram for each file
create_histogram <- function(text_data, file_name) {
word_counts <- sapply(strsplit(text_data, " "), length)
df <- data.frame(Line = 1:length(word_counts), Word_Counts = word_counts)
ggplot(df, aes(x = Word_Counts)) +
geom_histogram(binwidth = 1, fill = "blue", color = "black", alpha = 0.7) +
labs(title = paste("Word Count Distribution -", file_name),
x = "Words per Line", y = "Frequency") +
theme_minimal()
}
plot1 <- create_histogram(text1, "File 1")
plot2 <- create_histogram(text2, "File 2")
plot3 <- create_histogram(text3, "File 3")
# Save plots as images or view in RStudio
# For example: ggsave("file1_histogram.png", plot1)
print(plot1)
print(plot2)
print(plot3)
# Write a brief summary for a manager
write_summary_report <- function() {
cat("Exploratory Analysis Report\n")
cat("--------------------------\n")
cat("Three text files were analyzed to provide insights into their structure:\n\n")
cat("1. Summary Statistics:\n")
print(summary_table)
cat("\n2. Sample Data from Each File:\n")
print(basic_tables)
cat("\n3. Visualizations:\n")
cat("Histograms were created to illustrate word count distributions per line for each file.\n")
cat("These provide a quick understanding of text density and variability across the datasets.\n")
cat("\nThe analysis is presented concisely to assist decision-making for non-technical stakeholders.\n")
}
write_summary_report()
## Exploratory Analysis Report
## --------------------------
## Three text files were analyzed to provide insights into their structure:
##
## 1. Summary Statistics:
## File Word_Count Line_Count
## 1 File 1 37334131 899288
## 2 File 2 2643969 77259
## 3 File 3 30373543 2360148
##
## 2. Sample Data from Each File:
## $File1
## [1] "In the years thereafter, most of the Oil fields and platforms were named after pagan “gods”."
## [2] "We love you Mr. Brown."
## [3] "Chad has been awesome with the kids and holding down the fort while I work later than usual! The kids have been busy together playing Skylander on the XBox together, after Kyan cashed in his $$$ from his piggy bank. He wanted that game so bad and used his gift card from his birthday he has been saving and the money to get it (he never taps into that thing either, that is how we know he wanted it so bad). We made him count all of his money to make sure that he had enough! It was very cute to watch his reaction when he realized he did! He also does a very good job of letting Lola feel like she is playing too, by letting her switch out the characters! She loves it almost as much as him."
## [4] "so anyways, i am going to share some home decor inspiration that i have been storing in my folder on the puter. i have all these amazing images stored away ready to come to life when we get our home."
## [5] "With graduation season right around the corner, Nancy has whipped up a fun set to help you out with not only your graduation cards and gifts, but any occasion that brings on a change in one's life. I stamped the images in Memento Tuxedo Black and cut them out with circle Nestabilities. I embossed the kraft and red cardstock with TE's new Stars Impressions Plate, which is double sided and gives you 2 fantastic patterns. You can see how to use the Impressions Plates in this tutorial Taylor created. Just one pass through your die cut machine using the Embossing Pad Kit is all you need to do - super easy!"
##
## $File2
## [1] "He wasn't home alone, apparently."
## [2] "The St. Louis plant had to close. It would die of old age. Workers had been making cars there since the onset of mass automotive production in the 1920s."
## [3] "WSU's plans quickly became a hot topic on local online sites. Though most people applauded plans for the new biomedical center, many deplored the potential loss of the building."
## [4] "The Alaimo Group of Mount Holly was up for a contract last fall to evaluate and suggest improvements to Trenton Water Works. But campaign finance records released this week show the two employees donated a total of $4,500 to the political action committee (PAC) Partners for Progress in early June. Partners for Progress reported it gave more than $10,000 in both direct and in-kind contributions to Mayor Tony Mack in the two weeks leading up to his victory in the mayoral runoff election June 15."
## [5] "And when it's often difficult to predict a law's impact, legislators should think twice before carrying any bill. Is it absolutely necessary? Is it an issue serious enough to merit their attention? Will it definitely not make the situation worse?"
##
## $File3
## [1] "How are you? Btw thanks for the RT. You gonna be in DC anytime soon? Love to see you. Been way, way too long."
## [2] "When you meet someone special... you'll know. Your heart will beat more rapidly and you'll smile for no reason."
## [3] "they've decided its more fun if I don't."
## [4] "So Tired D; Played Lazer Tag & Ran A LOT D; Ughh Going To Sleep Like In 5 Minutes ;)"
## [5] "Words from a complete stranger! Made my birthday even better :)"
##
##
## 3. Visualizations:
## Histograms were created to illustrate word count distributions per line for each file.
## These provide a quick understanding of text density and variability across the datasets.
##
## The analysis is presented concisely to assist decision-making for non-technical stakeholders.