I'm working with an R data frame where I have columns representing "year" and "sales". I want to display these values as whole numbers without any decimal places. Currently, when I create a table or print the data frame, the "sales" values are displayed with decimal places (e.g., 10.00) and the "year" values are also showing decimal parts (e.g., 2019.00).
I'd like to know how to format these columns so that they are displayed as integers without any decimal places. I've tried using the format() function, but it doesn't seem to affect these specific columns.
Here's a snippet of my current code:
# Data
sales <- c(10, 30, 50, 20, 40)
revenue <- c(100.25, 300.50, 500.75, 200.00, 400.50)
profit <- c(10.4, 200.7, 400.5, 100.00, 356.79)
year <- c(2019:2023)
data <- rbind(Year = year, Sales = sales, Revenue = revenue, Profit = profit)
# Create a new table with different rows
Table <- kbl(data,
caption = "Development",
booktabs = TRUE,
linesep = c("\\addlinespace[0.2cm]"),
align = c("l", "r")) %>%
kable_styling(latex_options = c("striped"), font_size = 10, position = "left")
Table
Any guidance on how to achieve this formatting for "sales" and "year" columns would be greatly appreciated. Thank you in advance for your help!
Not sure how well included reprex describes your actual dataset format. If you start with Year / Sales / Revenue / Profit columns, you could first format all columns as strings, transpose to get a column for every year in a character matrix and optionally turn it back to data.frame (
kbl()will handle a matrix as well ).Created on 2023-08-31 with reprex v2.0.2