Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labeling with Hmisc::label() #26

Open
dylanrussellmd opened this issue Aug 30, 2020 · 5 comments
Open

Labeling with Hmisc::label() #26

dylanrussellmd opened this issue Aug 30, 2020 · 5 comments

Comments

@dylanrussellmd
Copy link

Is there anyway to replace the variable names in the output table with labels assigned to data frame columns by Hmisc::label()? The table1 package does this nicely.

@TysonStanley
Copy link
Owner

Great question. It should be possible using the `` in the table1() function. For example:

table1(
  data, 
  var1, var2, var3,
  var_names = Hmisc::label(
    select(data, var1, var2, var3)
  ))

This is a feature I could add to the package but won't have time for a bit.

@dylanrussellmd
Copy link
Author

This works very well! I think it would be a great addition. The only label it does not change is the splitby variable.

@dylanrussellmd
Copy link
Author

dylanrussellmd commented Sep 1, 2020

I have made a wrapper for your table1 function called tableone(). It converts logicals to factors with yes and no levels and allows the use of Hmisc::labeling. The only thing I can not get it to do yet is update the label of the splitby variable (in the example below, this would be cut).

tableone <- function(df, ..., group = NULL, test = TRUE) {
 args <- rlang::ensyms(...)
 group <- rlang::enexpr(group)

 change_to_factor <- function(col) {
   label <- Hmisc::label(col)
   col <- factor(col, levels = c(FALSE, TRUE), labels = c("No","Yes"))
   Hmisc::label(col) <- label
   return(col)
 }

 var_names <- function(df, ...) {
   Hmisc::label(dplyr::select(df, ...))
 }

  if(!is.null(group)) {
    df <- df %>%
      dplyr::select(!!!args, !!group) %>%
      dplyr::mutate(dplyr::across(where(is.logical), ~change_to_factor(.x)))
    rlang::eval_tidy(rlang::quo(furniture::table1(df, !!!args, splitby = ~!!group, test = TRUE, var_names = var_names(df, !!!args))))
  } else {
    df <- df %>% dplyr::select(!!!args) %>% dplyr::mutate(dplyr::across(where(is.logical), ~change_to_factor(.x)))
    rlang::eval_tidy(rlang::quo(furniture::table1(df, !!!args, var_names = var_names(df, !!!args))))
  }
}
diamonds <- ggplot2::diamonds
diamonds$expensive <- diamonds$price > 500
Hmisc::label(diamonds$depth) <- "Depth"
Hmisc::label(diamonds$table) <- "Table"
Hmisc::label(diamonds$price) <- "Price"
Hmisc::label(diamonds$clarity) <- "Clarity"
Hmisc::label(diamonds$cut) <- "Cut"
Hmisc::label(diamonds$expensive) <- "Expensive"
diamonds %>% tableone(depth, table, price, clarity, expensive, group = cut)

@TysonStanley
Copy link
Owner

Nice! I like this. Thanks for sharing! I may come back to this soon and think about how to implement this in the package, if you don't mind.

@dylanrussellmd
Copy link
Author

dylanrussellmd commented Sep 2, 2020

Nice! I like this. Thanks for sharing! I may come back to this soon and think about how to implement this in the package, if you don't mind.

Of course! Hopefully you can figure out how to work in printing the label for the 'splitby' variable too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants