Word Analytics
Part of the Logic & Flow section of Coddy's R journey. Lesson 62 of 64.
Challenge
HardComplete word_stats(text). Lower-case the text and remove the characters ., ,, ! and ?, then split it into words at runs of spaces. Return four lines:
Words:with the number of wordsDistinct:with the number of different wordsTop:with the three most frequent words and their counts, such asthe (3), and (2), bird (1), ordered by count from high to low and alphabetically among equal counts (fewer than three when the text has fewer words)Longest:with the longest word and its length in brackets, the first one in the text when several tie
The supplied code reads the text and prints each returned line.
Try it yourself
word_stats <- function(text) {
# Write your code here
character(0)
}
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
cat(word_stats(input[1]), sep = "\n")
All lessons in Logic & Flow
1Strings In Depth
Substrings with substr()Formatting with sprintf()Splitting and JoiningSearching StringsReplacing TextRecap - Username Builder4Matrices
Creating MatricesIndexing MatricesRow and Column SummariesMatrix ArithmeticRecap - Seating Chart2Key-Value Lookups
Named Vector LookupsChecking KeysAdding and Removing KeysLooping Over NamesRecap - Stock Desk3Sets and Counting
Unique ValuesSet OperationsMembership TestsCounting with table()Recap - Event Guests6Functions as Values
Anonymous FunctionsPassing FunctionsReturning FunctionsClosures with StateRecap - Discount Rules9Data Frames
Creating Data FramesColumns and RowsFiltering RowsAdding and SortingRecap - Sales Report12Project - Expense Tracker
Recording ExpensesTotal SpendingPractice on your own: Online R compiler