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

add_p() doesn't work for difftime values #880

Closed
oranwutang opened this issue Apr 29, 2021 · 1 comment · Fixed by #881
Closed

add_p() doesn't work for difftime values #880

oranwutang opened this issue Apr 29, 2021 · 1 comment · Fixed by #881

Comments

@oranwutang
Copy link
Contributor


Hi!

I have detected that add_p() doesn't work with columns of class difftime. As a matter of fact it is related with wilcox.test() which doesn't accept that class, but if the variable is parsed to numeric class with as.numeric() before using it on gtsummary workflow, it does work.

So my suggestion is that add_p() should check if variables are of difftime class and in that case parse them with as.numeric() before inputting them into wilcox.test().

PS: t.test() does accept difftime objects

This issue is somewhat related with (#343).

Thanks in advance!

(Sorry for the horrible reprex which is rendered with the html code included, but at least the tables are rendered)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(gtsummary)

set.seed(123)

time_diff <- lubridate::as.difftime(rnorm(250, 2000, 100), units = "mins")
group_sample <- sample(c("A", "B"), 250, replace = TRUE)

df <- data.frame(group_sample, time_diff)

df %>% select(group_sample, time_diff) %>% 
  tbl_summary(by=group_sample) %>% add_p()
#> There was an error in 'add_p()/add_difference()' for variable 'time_diff', p-value omitted:
#> Error in wilcox.test.default(x = structure(c(2012.92877351609, 2171.50649868833, : 'x' must be numeric
<style>html { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif; } #zqwufjmfih .gt_table { display: table; border-collapse: collapse; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; } #zqwufjmfih .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #zqwufjmfih .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; border-bottom-color: #FFFFFF; border-bottom-width: 0; } #zqwufjmfih .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 0; padding-bottom: 4px; border-top-color: #FFFFFF; border-top-width: 0; } #zqwufjmfih .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #zqwufjmfih .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #zqwufjmfih .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; } #zqwufjmfih .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; } #zqwufjmfih .gt_column_spanner_outer:first-child { padding-left: 0; } #zqwufjmfih .gt_column_spanner_outer:last-child { padding-right: 0; } #zqwufjmfih .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; overflow-x: hidden; display: inline-block; width: 100%; } #zqwufjmfih .gt_group_heading { padding: 8px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; } #zqwufjmfih .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle; } #zqwufjmfih .gt_from_md > :first-child { margin-top: 0; } #zqwufjmfih .gt_from_md > :last-child { margin-bottom: 0; } #zqwufjmfih .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden; } #zqwufjmfih .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 12px; } #zqwufjmfih .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #zqwufjmfih .gt_first_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; } #zqwufjmfih .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #zqwufjmfih .gt_first_grand_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; } #zqwufjmfih .gt_striped { background-color: rgba(128, 128, 128, 0.05); } #zqwufjmfih .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #zqwufjmfih .gt_footnotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #zqwufjmfih .gt_footnote { margin: 0px; font-size: 90%; padding: 4px; } #zqwufjmfih .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #zqwufjmfih .gt_sourcenote { font-size: 90%; padding: 4px; } #zqwufjmfih .gt_left { text-align: left; } #zqwufjmfih .gt_center { text-align: center; } #zqwufjmfih .gt_right { text-align: right; font-variant-numeric: tabular-nums; } #zqwufjmfih .gt_font_normal { font-weight: normal; } #zqwufjmfih .gt_font_bold { font-weight: bold; } #zqwufjmfih .gt_font_italic { font-style: italic; } #zqwufjmfih .gt_super { font-size: 65%; } #zqwufjmfih .gt_footnote_marks { font-style: italic; font-size: 65%; } </style>
Characteristic A, N = 1181 B, N = 1321 p-value
time_diff 1,992 (1,934, 2,063) 1,981 (1,938, 2,055)

1

      Median (IQR)
      <br />
    </p>
  </td>
</tr>

class(df$time_diff)
#> [1] "difftime"

t.test(df$time_diff~df$group_sample)
#>
#> Welch Two Sample t-test
#>
#> data: df$time_diff by df$group_sample
#> t = 0.38027 mins, df = 237.43, p-value = 0.7041 mins
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#> -19.11303 mins 28.25683 mins
#> sample estimates:
#> Time differences in mins
#> mean in group A mean in group B
#> 2001.558 1996.986

wilcox.test(df$time_diff~df$group_sample)
#> Error in wilcox.test.default(x = structure(c(2012.92877351609, 2171.50649868833, : 'x' must be numeric

df$time_diff <- as.numeric(df$time_diff)

df %>% select(group_sample, time_diff) %>%
tbl_summary(by=group_sample) %>% add_p()

<style>html { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif; } #apvbhnlzhi .gt_table { display: table; border-collapse: collapse; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; } #apvbhnlzhi .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #apvbhnlzhi .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; border-bottom-color: #FFFFFF; border-bottom-width: 0; } #apvbhnlzhi .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 0; padding-bottom: 4px; border-top-color: #FFFFFF; border-top-width: 0; } #apvbhnlzhi .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #apvbhnlzhi .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #apvbhnlzhi .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; } #apvbhnlzhi .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; } #apvbhnlzhi .gt_column_spanner_outer:first-child { padding-left: 0; } #apvbhnlzhi .gt_column_spanner_outer:last-child { padding-right: 0; } #apvbhnlzhi .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; overflow-x: hidden; display: inline-block; width: 100%; } #apvbhnlzhi .gt_group_heading { padding: 8px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; } #apvbhnlzhi .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle; } #apvbhnlzhi .gt_from_md > :first-child { margin-top: 0; } #apvbhnlzhi .gt_from_md > :last-child { margin-bottom: 0; } #apvbhnlzhi .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden; } #apvbhnlzhi .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 12px; } #apvbhnlzhi .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #apvbhnlzhi .gt_first_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; } #apvbhnlzhi .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #apvbhnlzhi .gt_first_grand_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; } #apvbhnlzhi .gt_striped { background-color: rgba(128, 128, 128, 0.05); } #apvbhnlzhi .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #apvbhnlzhi .gt_footnotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #apvbhnlzhi .gt_footnote { margin: 0px; font-size: 90%; padding: 4px; } #apvbhnlzhi .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #apvbhnlzhi .gt_sourcenote { font-size: 90%; padding: 4px; } #apvbhnlzhi .gt_left { text-align: left; } #apvbhnlzhi .gt_center { text-align: center; } #apvbhnlzhi .gt_right { text-align: right; font-variant-numeric: tabular-nums; } #apvbhnlzhi .gt_font_normal { font-weight: normal; } #apvbhnlzhi .gt_font_bold { font-weight: bold; } #apvbhnlzhi .gt_font_italic { font-style: italic; } #apvbhnlzhi .gt_super { font-size: 65%; } #apvbhnlzhi .gt_footnote_marks { font-style: italic; font-size: 65%; } </style>
Characteristic A, N = 1181 B, N = 1321 p-value2
time_diff 1,992 (1,934, 2,063) 1,981 (1,938, 2,055) 0.9

1

      Median (IQR)
      <br />
    </p>
    <p class="gt_footnote">
      <sup class="gt_footnote_marks">
        <em>2</em>
      </sup>
       
      Wilcoxon rank sum test
      <br />
    </p>
  </td>
</tr>

Created on 2021-04-29 by the reprex package (v0.3.0)

@ddsjoberg
Copy link
Owner

Hey @oranwutang ! Can you review this pull request? #881
There is a checklist of items to review :)

library(gtsummary)

set.seed(123)

time_diff <- lubridate::as.difftime(rnorm(250, 2000, 100), units = "mins")
group_sample <- sample(c("A", "B"), 250, replace = TRUE)

df <- data.frame(group_sample, time_diff)

df %>% 
  select(group_sample, time_diff) %>% 
  tbl_summary(by=group_sample) %>%
  add_p() %>%
  as_kable()
Characteristic A, N = 118 B, N = 132 p-value
time_diff 1,992 (1,934, 2,063) 1,981 (1,938, 2,055) 0.9

Created on 2021-04-29 by the reprex package (v2.0.0)

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

Successfully merging a pull request may close this issue.

2 participants