gdemin / expss

expss: Tables and Labels in R
https://cran.r-project.org/web/packages/expss/
84 stars 16 forks source link

No sort special values #76

Closed robertogilsaura closed 3 years ago

robertogilsaura commented 3 years ago

Hello @gdemin, my best wishes for Christmas and new year. I really hope that 2021 is a better year for everyone.

I have a question about tab_sort()

m_1 <- c(1:50, 99, 99, 99, 99, 99, 99, 99, 99)
m_2 <- c(1:50, NA, NA, NA, NA, NA, NA, NA, NA)
df <- data.frame(m_1,m_2)

df %>% 
     tab_cells(mrset_f(m_)) %>% 
     tab_stat_cases() %>% 
     tab_pivot() %>% 
     tab_sort_desc()

Is there any way in order to fix 99 in bottom of crosstab? You know, 99 "none of them" or " all of them" or DA or DK in the bottom of table (no sorting missing or special values)

Thanks in advance Regards

gdemin commented 3 years ago

Hello @robertogilsaura, merry Christmas,

There is a special argument in the tab_sort_desc: excluded_rows. It accepts regular expression and rows which match this regular expression won't be sorted. By default it is '#' because we don't need to sort totals. And we can add additional patterns here with | ('or' in regular expression):

df %>% 
    tab_cells(mrset_f(m_)) %>% 
    tab_stat_cases() %>% 
    tab_pivot() %>% 
    tab_sort_desc(excluded_rows = "#|99")

If you have labels rather than code you need to specify labels: "#|Hard to say|Don\'t know"

robertogilsaura commented 3 years ago

it runs properly!!

Thanks.