🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of katrinleinweber

katrinleinweber's solution

to Word Count in the R Track

Published at Jul 13 2018 · 0 comments
Test suite

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1


See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.R containing a function that returns a NULL value. Place your implementation inside the body of the function.

How to run tests

Inside of RStudio, simply execute the test_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. Because exercism code and tests are in the same folder, use this same path for both code_path and test_path parameters. On the command-line, you can also run Rscript test_<exercise_name>.R.


This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.



context("word count")

# When comparing lists, all.equal expects the objects to be in the same order
# This expectation instead checks that a) the set of names are the same and
# b) each named object is equal
expect_equal_pairs <- function(object, expected) {
               info = "names in lists differ")
  for (name in names(expected)) {
    expect_equal(object[name], expected[name], info = "list element missing")

test_that("count one word", {
                     list("word" = 1))

test_that("count one of each word", {
  expect_equal_pairs(word_count("one of each"),
                       "one" = 1,
                       "of" = 1,
                       "each" = 1

test_that("multiple occurrences of a word", {
    word_count("one fish two fish red fish blue fish"),
      "one" = 1,
      "fish" = 4,
      "two" = 1,
      "red" = 1,
      "blue" = 1

test_that("ignore punctuation", {
    word_count("car : carpet as java : javascript!!&@$%^&"),
      "car" = 1,
      "carpet" = 1,
      "as" = 1,
      "java" = 1,
      "javascript" = 1

test_that("include numbers", {
  expect_equal_pairs(word_count("testing, 1, 2 testing"),
                       "testing" = 2,
                       "1" = 1,
                       "2" = 1

test_that("normalize case", {
  expect_equal_pairs(word_count("go Go GO Stop stop"),
                     list("go" = 3, "stop" = 2))

test_that("multiple whitespaces", {
  expect_equal_pairs(word_count(" multiple   whitespaces "),
                     list("multiple" = 1, "whitespaces" = 1))

message("All tests passed for exercise: word-count")

word_count <- function(input) {
  string <- tolower(input)
  # extract unique words, so that counting works without duplicates
  string %>% 
    stri_extract_all_words() %>% 
    unlist() %>% 
    unique() -> 
  # count each word in string by utilising word boundary reg-ex
  count <- function(w) {
    string %>% 
      stri_count_regex(paste0(w, "\\b"))
  # construct results list
  counts <- purrr::map_int(words, count)
  names(counts) <- words

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?