Include specific variables in sumtable

Hello guys,

I would like to create a table of descriptive statistics with the "sumtable" function. However, I would like to only include specific varibles, especially since the dataset includes nearly 200 of them. How do I select only specific variables for the summary statistics?

If another package does the same job just as good, that's of course also fine.

Thanks in advance!

Complete statistics in one table. first browse and select your CSV file, then as you check the variable on the left side, statistics will appear. hope it helps

##################################
library(shiny)
library(shinyWidgets)
library(DT) # for using %>% which works as a pipe in R code
library(dplyr) # select functions are covered in the library
library(Rmisc) # to calculate confidence interval
library(moments) # to calculate skewness,Kurtosis in r
library(kableExtra) # to handle scroll_box function

Create the function.

getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}

ui <- fluidPage(
column(style = "border: 4px double red;height: 500px;overflow-y: auto;",
width = 2,
br(),
fileInput("file",
label = "Select: ONLY csv",
multiple = FALSE,
accept = c("csv",
"comma-separated-values,text",
".csv")),
x <- uiOutput('radio_Btns')
),
column(style = "border: 4px double red;height: 500px;overflow-y: auto;",
width = 10,
htmlOutput('msummarytext')
)
)

server <- function(input, output, session) {
vmy <- reactiveValues(mydata=NULL,zzDevic=NULL)

observeEvent(input$file,{
ext <- tools::file_ext(input$file$name)
vmy$mydata <- as.data.frame(read.csv(input$file$datapath,header = T))
vmy$mydata <- dplyr::select_if(vmy$mydata,is.numeric)
fnCreatezzDevictbl()

})

output$msummarytext <- renderUI({
  if (length(input$file)==0){
    return()
  }
n<- ncol(vmy$zzDevic)

options(knitr.table.format = "html") 



HTML( kable(vmy$zzDevic[vmy$zzDevic$Variable %in% c(input$radioreply), ], 
            escape = F,caption ="Descriptive Statistics",row.names=FALSE,
            table.attr = "style='width:30%;'")%>%
        kable_styling(font_size = 14, position = "center", html_font = "Cambria",fixed_thead = T) %>%
        kable_paper("striped", full_width = F) %>% 
        column_spec(c(1,n), background = "steelblue",color = "white")%>%
        row_spec(0, angle = 360,bold=TRUE)%>%
        column_spec((1:n), bold = T,border_left = TRUE,border_right = TRUE) %>%
        kableExtra::scroll_box(width = "100%", height = "385px")
)

})

output$radio_Btns <- renderUI({
if (length(input$file)==0){
return()
}
options <- colnames(vmy$mydata) # The options are dynamically generated on the server
prettyCheckboxGroup(
inputId = "radioreply",
label = "Select variable to Report Stat",
choices = options,
selected = options[1],
inline = FALSE,
status = "danger"
)# radioGroupbtn closure
}) # renderUI closure

fnCreatezzDevictbl <- function(){
vmy$zzDevic<<- data.frame (Variable=character(),
Mean =double(),
Median =double(),
Mode = double(),
SD=double(),
U_CI = double(),
L_CI = double(),
Conf_Intrl = double(),
Std_Error =double(),
Coff_Var = double(),
Inter_Qtrl = double(),
First_Qtrl=double(),
Third_Qtrl =double(),
Minimum =double(),
Maximum =double(),
Skewness=double(),
Kurtosis=double(),
Variable2 = character()
)

for (i in names(vmy$mydata)){
    x<- vmy$mydata[ ,i]
    vmy$zzDevic<- vmy$zzDevic %>% add_row(
      Variable    = i,
      Mean        = round(mean(x,na.rm = TRUE),3),
      Minimum     = round(min(x,na.omit(x)),3),
      First_Qtrl  = round(quantile(x, prob=c(0.25)),3),
      Median      = round(median(x,na.rm = TRUE),3),
      Third_Qtrl  = round(quantile(x, prob=c(0.75)),3),
      Maximum     = round(max(x,na.omit(x)),3),
      SD          = round(sd(x, na.rm=TRUE),3),
      U_CI        = round(CI(x,ci = 0.95)[1],3),
      L_CI        = round(CI(x,ci = 0.95)[3],3),
      Conf_Intrl  = round(CI(x,ci = 0.95)[1] - mean(x,na.rm = TRUE),3),
      Skewness    = round(skewness(x,na.rm = TRUE),3),
      Kurtosis    = round(kurtosis(x,na.rm = TRUE),3),
      Inter_Qtrl  = round(IQR(x,na.rm = TRUE),3),
      Std_Error   = round(sd(x, na.rm =TRUE)/sqrt(length(na.omit(x))),3),
      Coff_Var   = round(sd(x, na.rm =TRUE)/mean(x,na.rm = TRUE),3),
      Mode        = round(getmode(x),3),
      Variable2   = i
    )
}

}
}
shinyApp(ui, server)

Hello,

it is actually an .dta file, so a STATA file.
My intention was to export the descriptive statistics to embed them in a word file ^^

I looked at the function documentation and it has a vars param. You could use that.
sumtable: Summary Statistics (r-project.org)

data and vars

The data argument can take any data.frame , data.table , tibble , or matrix , as long as it has a valid set of variable names stored in the colnames() attribute.

By default, sumtable will include in the summary statistics table every variable in the data set that is (1) numeric, (2) factor, (3) logical, or (4) a character variable with six or fewer unique values (as a factor), and it will include them in the order they’re in the data.

You can override that variable list with vars , which is just a vector of variable names to include. You can use this to force sumtable to ignore variables you don’t want, or to include variables it doesn’t by default.

data(LifeCycleSavings)
st(LifeCycleSavings, vars = c('pop15','pop75'))
1 Like

Thank you very much, it works now perfectly well!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.