0

I'm trying to use the gt package in R to make a summary table. My data are structured as:

Value Condition_1 Condition_2 Type
0.33      LN          0.5      <10%
0.43      LN          0.5      <20%
0.23      LN          0.5      >20%
0.43      LN          0.6      <10%
0.23      LN          0.6      <20%
0.33      LN          0.6      >20%
0.33      LS          0.5      <10%
0.43      LS          0.5      <20%
0.23      LS          0.5      >20%
0.43      LS          0.6      <10%
0.23      LS          0.6      <20%
0.33      LS          0.6      >20%

I'd like the resulting table to look like:

Type <10% <20% >20%
LN-0.5
     0.33  0.43 0.23
LN-0.6
     0.43  0.23 0.33
LS-0.5
     0.33  0.43 0.23
LS-0.6
     0.43  0.23 0.33

I'm getting pretty close I think, and currently have my code set up as:

df %>% gt(rowname_col = c("value"),
       groupname_col = c("Condition_1", "Condition_2")) %>% 
       summary_rows(groups = TRUE,
       columns = vars(value),
       fns = list(avg = ~mean(.)))   

I thought it would be as simple as swapping columns = vars(value) for columns = vars(Type) but that option can only handle numeric vectors (as far as I can tell). Can anyone please help figure this out? Thanks in advance!

1 Answer 1

1

Does this look closer to what you expect? I don't think I perfectly recreated your dataset, but perhaps close enough for proof of concept. Started by wrangling your data to combine Condition_1 and Condition_2, then made the data wider, then passed to gt().

library(gt)
library(tidyverse)

df <- data.frame(Value = c(0.33, 0.43, 0.23, 0.43, 0.23, 0.33, 0.33, 0.43, 0.23, 0.43, 0.23, 0.33), 
                         Condition_1 = c('LN', 'LN', 'LN', 'LN', 'LN', 'LN', 'LS', 'LS', 'LS', 'LS', 'LS', 'LS'), 
                         Condition_2 = c(0.5, 0.5, 0.5, 0.6, 0.6, 0.6, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6), 
                         Type = c('<10%', '<20%', '<20%', '<10%', '<10%', '<20%', '>20%', '<10%', '<20%', '>20%', '<10%', '<20%')) %>% 
            mutate(Condition = paste0(Condition_1, '-', Condition_2)) %>% 
              select(Type, Value, everything()) %>% 
              pivot_wider(names_from = Type, 
                          values_from = Value)
          
          df %>% 
            gt(groupname_col = 'Condition') %>% 
              cols_hide(column = vars(Condition_1, Condition_2))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.