2

Suppose I have dataframe 'y'

WR<-c("S",'J',"T")
B<-c("b1","b2","b3")
wgt<-c(0.3,2,3)
y<-data.frame(WR,B,wgt)

I want to make column percentage crosstab with B as row, WR, and total of WR as columns using expss function

library(expss)

y %>% tab_cols(total(),WR) %>% # Columns
    tab_stat_valid_n("Base") %>% 
    tab_weight(wgt) %>% 
    tab_stat_valid_n("Projection") %>%
     tab_cells(mrset(B))%>%   # Row
    tab_stat_cpct(total_row_position = "none") %>%
    tab_pivot() 

Result

But the total Base column does not match up

#                      #Total    WR|J  WR|S WR|T
#                Base  1.000000    1   1.0    1
#          Projection  5.300000    2   0.3    3
#                  b1  5.660377   NA 100.0   NA
#                  b2 37.735849  100    NA   NA
#                  b3 56.603774   NA    NA  100

I think I found the solution

y %>% tab_cols(total(),WR) %>% # Columns
    tab_cells(mrset(B))%>%   # Row 
    tab_stat_valid_n("Base") %>% 
    tab_weight(wgt) %>% 
    tab_stat_valid_n("Projection") %>%
    tab_stat_cpct(total_row_position = "none") %>%
    tab_pivot() 

 |    |            | #Total |  WR |       |     |
 |    |            |        |   J |     S |   T |
 | -- | ---------- | ------ | --- | ----- | --- |
 |  B |       Base |    3.0 |   1 |   1.0 |   1 |
 |    | Projection |    5.3 |   2 |   0.3 |   3 |
 | b1 |            |    5.7 |     | 100.0 |     |
 | b2 |            |   37.7 | 100 |       |     |
 | b3 |            |   56.6 |     |       | 100 |
Szicocs
  • 21
  • 2
  • 2
    Please provide: https://stackoverflow.com/help/minimal-reproducible-example Also, which packages are you using here? – deschen Mar 16 '22 at 09:53
  • thanks for the suggestion, I have edited my question, hope things will become clearer. – Szicocs Mar 17 '22 at 02:58
  • What do you mean with „base column does not match up“? – deschen Mar 17 '22 at 07:07
  • 1
    # Total column in Row should be 3.. right?, nvm, I just found out putting tab_cell() after, tab_cols() , fixed this problem... – Szicocs Mar 18 '22 at 03:37

0 Answers0