Wide table to long table

Solution 1:

While a tibble can have row names (e.g., when converting from a regular data frame), they are removed when subsetting with the [ operator. A warning will be raised when attempting to assign non-NULL row names to a tibble.

Generally, it is best to avoid row names, because they are basically a character column with different semantics than every other column. https://tibble.tidyverse.org/reference/rownames.html

In your case pivot_longer is removing the rownames, but you could save the rownames as column with rownames_to_column from tibble package before transforming with pivot_longer like this:

library(tibble)
library(tidyr)
library(dplyr)

result_long <- result %>% 
  rownames_to_column("id") %>% 
  pivot_longer(
    -id, 
    names_to="combination", 
    values_to ="price.m" 
  )
A tibble: 21 x 3
   id            combination price.m
   <chr>         <chr>         <dbl>
 1 Above average D             2986.
 2 Above average E             3020.
 3 Above average F             3009.
 4 Above average G             2783.
 5 Above average H             2874.
 6 Above average I             2592.
 7 Above average J             3051.
 8 Below average D             2843.
 9 Below average E             2944.
10 Below average F             3142.
# ... with 11 more rows

Solution 2:

In base R, we may convert to table and wrap with as.data.frame

as.data.frame.table(as.matrix(result))

-output

            Var1 Var2     Freq
1  Above average    D 2986.286
2  Below average    D 2842.540
3      very Good    D 2921.000
4  Above average    E 3020.458
5  Below average    E 2943.926
6      very Good    E 2860.763
7  Above average    F 3008.644
8  Below average    F 3142.134
...