0

I have a small dataframe of dimension 2 x 8 (see below) and I'd like to create a simple white table with the row names "scenario 1" and "scenario 2".

How can I do this as simple as possible? Could someone also show me how to swap the order of the columns?

dput:

structure(list(A = list(0.934842767295597, 0.92922841571852), 
    B = list(0.516589250165893, 0.525067529460655), C = list(
        0.867615330138182, 0.866211826128489), D = list(0.0881670095698295, 
        0.0834131044726853), E = list(0.85734568558549, 0.856776377557092), 
    F = list(1.02523463957426, 1.01379194951716), G = list(0.444709565304963, 
        0.419220178831526), H = c(0.22, 0.25)), row.names = c(18L, 
21L), class = "data.frame")

Thank you!

EDIT: The output should be a table with 16 values (2x8). The column 'H' should be on the left. The first row label should be 'scenario 1' and the second row label should be 'scenario 2' (these labels are currently not included in my example). The values should be rounded to two decimals. I do not want to create a fancy table, but rather a simple table that can be saved as a pdf.

s28
  • 173
  • 5
  • 1
    Could edit your question to include an example of your desired output? – jpsmith Jun 29 '23 at 18:51
  • 1
    This is unclear. I see nothing in the row names or column names that would imply "scenarios". If this is an action of summarizing columns, see https://stackoverflow.com/q/11562656/3358272, https://stackoverflow.com/q/1660124/3358272, https://stackoverflow.com/q/12064202/3358272. If this is about reshaping it, see pivot long-to-wide: https://stackoverflow.com/q/5890584/3358272, https://stackoverflow.com/q/11608167/3358272, or pivot wide-to-long: https://stackoverflow.com/q/2185252/3358272, https://stackoverflow.com/q/68058000/3358272. Otherwise, please [edit] your question and clarify. Thanks! – r2evans Jun 29 '23 at 18:53
  • 1
    (The use of *"simple white table"* might suggest that you are trying to render this into a fancy-looking table within a report, webpage, or something similar. In that case, you'll need to give a lot more context including the target format and mechanisms you're intending to use to get there. For instance, R markdown (using `rmarkdown` and `knitr`) to docx or html or pdf.) – r2evans Jun 29 '23 at 18:54

1 Answers1

1

Somethink like?:

df <- structure(list(A = list(0.934842767295597, 0.92922841571852), 
               B = list(0.516589250165893, 0.525067529460655), C = list(
                 0.867615330138182, 0.866211826128489), D = list(0.0881670095698295, 
                                                                 0.0834131044726853), E = list(0.85734568558549, 0.856776377557092), 
               F = list(1.02523463957426, 1.01379194951716), G = list(0.444709565304963, 
                                                                      0.419220178831526), H = c(0.22, 0.25)), row.names = c(18L, 
                                                                                                                            21L), class = "data.frame")

rownames(df) <- c("scenario 1", "scenario 2")
df |>
  knitr::kable()
A B C D E F G H
scenario 1 0.9348428 0.5165893 0.8676153 0.08816701 0.8573457 1.025235 0.4447096 0.22
scenario 2 0.9292284 0.5250675 0.8662118 0.0834131 0.8567764 1.013792 0.4192202 0.25

Created on 2023-06-29 with reprex v2.0.2

Otherwise please follow @r2evans suggestion and provide more context.

Grzegorz Sapijaszko
  • 1,913
  • 1
  • 5
  • 12