In the age of Rmarkdown and Shiny, or when making any custom output from your data you want your output to look consistent and neat. Also, when writing your output you often want it to obtain a specific (decorative) format defined by the html or LaTeX engine. These exercises are an opportunity to refresh our memory on functions such as paste, sprintf, formatC and others that are convenient tools to achieve these ends. All of the solutions rely partly on the ultra flexible sprintf() but there are no-doubt many ways to solve the exercises with other functions, feel free to share your solutions in the comment section.
Example solutions are available here.
Print out the following vector as prices in dollars (to the nearest cent):
c(14.3409087337707, 13.0648270623048, 3.58504267621646, 18.5077076398145,. Example:
Using these numbers
c(25, 7, 90, 16) make a vector of filenames in the following format:
file_025.txt. That is, left pad the numbers so they are all three digits.
Actually, if we are only dealing numbers less than hundred
file_25.txt would have been enough. Change the code from last exercise so that the padding is progammatically decided by the biggest number in the vector.
Print out the following haiku on three lines, right aligned, with the help of cat.
c("Stay the patient course.", "Of little worth is your ire.", "The network is down.").
Write a function that converts a number to its hexadecimal representation. This is a useful skill when converting bmp colors from one representation to another. Example output:
tohex(12)  "12 is c in hexadecimal"
Take a string and programmatically surround it with the html header tag
Back to the poem from exercise 4, let R convert to html unordered list. So that it would appear like the following in a browser:
- Stay the patient course.
- Of little worth is your ire.
- The network is down.
Here is a list of the currently top 5 movies on imdb.com in terms of rating
c("The Shawshank Redemption", "The Godfather", "The Godfather: Part II", "The Dark Knight", "12 Angry Men", "Schindler's List") convert them into a list compatible with written text.
 "The top ranked films on imdb.com are The Shawshank Redemption, The Godfather, The Godfather: Part II, The Dark Knight, 12 Angry Men and Schindler's List"
Now you should be able to solve this quickly: write a function that converts a proportion to a percentage that takes as input the number of decimal places. Input of 0.921313 and 2 decimal places should return
Improve the function from last exercise so the percentage take consistently 10 characters by doing some left padding. Raise an error if percentage already happens to be longer than 10.
(Image by Daniel Friedman).
Related exercise sets:
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…
Source:: R News