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 thesep
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
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 inpretty_income
, usesep = ""
, so there is no space between the$
and value. - Paste a
%
to the end of each value inpretty_percent
, usesep = ""
, so there is no space between the value and the%
. years
contains the year eachpretty_percent
corresponds to. Usepaste()
to produce a vector with elements like2010: +4.0%
and assign it toyear_percent
.- Use
paste()
withyear_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
___