A very simple table
Combining format() and paste() is one way to display very simple tables. Remember, since format() looks at all the values in a vector before formatting, it uses a consistent format and will, by default, align on the decimal point. This is usually the behavior you want for a column of numbers in table.
format() can also take character vectors as input. In this case, you can use the justify argument, specific to character input, to justify the text to the left, right, or center.
You are going to put together the following table:
Year 0 $ 72
Year 1 $ 1,030
Year 2 $ 10,292
Project Lifetime $1,189,192
You'll start by formatting the columns to prepare to put them in a table, then you'll use paste() to put together each row. Then, you can use writeLines() to display each row on a new line.
Este exercício faz parte do curso
String Manipulation with stringr in R
Instruções do exercício
The income vector is loaded in your workspace.
- Create
pretty_incomeby usingformat()withdigits = 2andbig.mark = ",". - Create
dollar_incomeby pasting$topretty_income(don't forget to set thesepargument). - Create
formatted_namesby usingformat()onincome_nameswithjustify = "right". - Create
rowsby pasting togetherformatted_namesanddollar_income. Use three spaces as a separator to give some room between your columns. Be sure to surround your separator in". - Call
writeLines()onrowsto see your table.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Define the names vector
income_names <- c("Year 0", "Year 1", "Year 2", "Project Lifetime")
# Create pretty_income
pretty_income <- ___
# Create dollar_income
dollar_income <- ___
# Create formatted_names
formatted_names <- ___
# Create rows
___
# Write rows
___