To pivot wider and have grouped variables appear as subheaders in a table in R, you can use the pivot_wider function from the tidyr package in combination with dplyr for data manipulation. Here's an example:
main.r502 chars24 lines
In this example, id is the column that we group by, year is the column that we pivot on, and sales and expenses are the columns that we want to pivot wider. The resulting table will have subheaders for each year and sales/expenses combination.
Note: Make sure to replace dataset with your actual dataset and adjust the id_cols, names_from, and values_from arguments according to your data.
Alternatively, you can use reshape function from base R, but pivot_wider from tidyr is generally easier to use and more flexible.
Output will look something like this:
main.r359 chars6 lines
gistlibby LogSnag