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 thesepargument 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
collapseif you want a single string as output.collapsespecifies the string to place between different elements.
Este exercício faz parte do curso
String Manipulation with stringr in R
Instruções do exercício
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%. yearscontains the year eachpretty_percentcorresponds to. Usepaste()to produce a vector with elements like2010: +4.0%and assign it toyear_percent.- Use
paste()withyear_percentto create single string that collapses all the years:2010: +4.0%, 2011: -1.9%, 2012: +3.0%, 2013: -5.0%.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Add $ to pretty_income
___
# Add % to pretty_percent
___
# Create vector with elements like 2010: +4.0%`
year_percent <- ___
# Collapse all years into single string
___