Annotation of numbers

To get a handle on using paste(), you are going to annotate some of your formatted number strings.

The key points to remember are:

  • The vectors you pass to paste() are pasted together element by element, using the sep argument to combine them.
  • If the vectors passed to paste() aren't the same length, the shorter vectors are recycled up to the length of the longest one.
  • Only use collapse if you want a single string as output. collapse specifies the string to place between different elements.

This exercise is part of the course

String Manipulation with stringr in R

View Course

Exercise instructions

We've put the formatted vectors pretty_income and pretty_percent in your workspace along with years.

  • Paste a $ to the front of each value in pretty_income, use sep = "", so there is no space between the $ and value.
  • Paste a % to the end of each value in pretty_percent, use sep = "", so there is no space between the value and the %.
  • years contains the year each pretty_percent corresponds to. Use paste() to produce a vector with elements like 2010: +4.0% and assign it to year_percent.
  • Use paste() with year_percent to create single string that collapses all the years: 2010: +4.0%, 2011: -1.9%, 2012: +3.0%, 2013: -5.0%.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Add $ to pretty_income
___
 
# Add % to pretty_percent
___

# Create vector with elements like 2010: +4.0%`
year_percent <- ___

# Collapse all years into single string
___