rggplot2expss

cross table with percentages and significant differences


I am trying to replicate this table in R, I did the above in Power Bi

enter image description here

try the following code because I have no idea where to start I have done everything but nothing works

library(expss)
as.datatable_widget(base %>%
                      group_by(CP1_TOTV2) %>%
                      summarize(weight = sum(weight)) %>%
                      tab_cells(weight / sum(weight)) %>%
                      tab_cols(total(), BANNER3) %>%
                      tab_stat_tpct(total_row_position = "above", total_label = "Total") %>% # aquí señalo los porcentajes total muestra
                      tab_pivot())

What I hope at the beginning is to form the same table and be able to add at the end a column that shows the significant differences between wave 2 and wave 3 per row, but I have not had any idea how to do it, I leave the data if you need to see how it is distributed Data in sheets


Solution

  • You might wanna look at data wrangling cheatsheet. To get the basics.

    It's hard to match the picture exactly since I don't know where the variables exactly are. But going by your comment, OP @yefersonG, here is an example of summarizing CP1_TOTV2.

    library(tidyverse)
    
    df=read_csv("df.csv")
    
    df=df%>%select(starts_with("CP1_TOTV"))
    
    
    df |> 
      pivot_longer(everything(),names_to = "CP1_TOTV")|>
      group_by(CP1_TOTV)|>
      summarise(Freq=sum(value=="Yes"))|>
      mutate(Percentage=Freq/sum(Freq),
             Percentage=scales::percent(Percentage))
    
    

    enter image description here