Giter Site home page Giter Site logo

Replicate tables about expss HOT 5 CLOSED

gdemin avatar gdemin commented on May 26, 2024
Replicate tables

from expss.

Comments (5)

gdemin avatar gdemin commented on May 26, 2024

Hi, @robertogilsaura
You can use tab_rows or tab_subgroup:

library(expss)
BRAND <- c(1,2,3,2,3,2,1,2,3,4,3,2,1,2,3,4,3,4)
LIKES <- c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,1)
GENDER <- c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)

cis <- data.frame(GENDER,BRAND,LIKES)
# with tab_rows
cis %>%
    tab_cells(LIKES) %>%
    tab_cols(total(), GENDER) %>%
    tab_rows(BRAND) %>% 
    tab_stat_cases() %>%
    tab_pivot()
 # |       |    |       |              | #Total | GENDER |    |
 # |       |    |       |              |        |      1 |  2 |
 # | ----- | -- | ----- | ------------ | ------ | ------ | -- |
 # | BRAND |  1 | LIKES |            1 |      3 |      2 |  1 |
 # |       |    |       |            2 |        |        |    |
 # |       |    |       | #Total cases |      3 |      2 |  1 |
 # |       |  2 | LIKES |            1 |        |        |    |
 # |       |    |       |            2 |      6 |      4 |  2 |
 # |       |    |       | #Total cases |      6 |      4 |  2 |
 # |       |  3 | LIKES |            1 |      6 |      3 |  3 |
 # |       |    |       |            2 |        |        |    |
 # |       |    |       | #Total cases |      6 |      3 |  3 |
 # |       |  4 | LIKES |            1 |      1 |        |  1 |
 # |       |    |       |            2 |      2 |        |  2 |
 # |       |    |       | #Total cases |      3 |        |  3 |

# and with tab_subgroup
for(i in sort(unique(cis$BRAND))){
    cis %>%
        tab_cells(LIKES) %>%
        tab_cols(total(), GENDER) %>%
        tab_subgroup(BRAND == i) %>% 
        tab_stat_cases() %>%
        tab_pivot() %>% 
        set_caption(sprintf("BRAND == %s", i)) %>% 
        print()
}
 # BRAND == 1                                                  
 # |       |              | #Total | GENDER |    |
 # |       |              |        |      1 |  2 |
 # | ----- | ------------ | ------ | ------ | -- |
 # | LIKES |            1 |      3 |      2 |  1 |
 # |       | #Total cases |      3 |      2 |  1 |
 # BRAND == 2                                                  
 # |       |              | #Total | GENDER |    |
 # |       |              |        |      1 |  2 |
 # | ----- | ------------ | ------ | ------ | -- |
 # | LIKES |            2 |      6 |      4 |  2 |
 # |       | #Total cases |      6 |      4 |  2 |
 # BRAND == 3                                                  
 # |       |              | #Total | GENDER |    |
 # |       |              |        |      1 |  2 |
 # | ----- | ------------ | ------ | ------ | -- |
 # | LIKES |            1 |      6 |      3 |  3 |
 # |       | #Total cases |      6 |      3 |  3 |
 # BRAND == 4                                             
 # |       |              | #Total | GENDER |
 # |       |              |        |      2 |
 # | ----- | ------------ | ------ | ------ |
 # | LIKES |            1 |      1 |      1 |
 # |       |            2 |      2 |      2 |
 # |       | #Total cases |      3 |      3 |

from expss.

robertogilsaura avatar robertogilsaura commented on May 26, 2024

hi @gdemin

a lot of thanks.... second way was the output that I was looking for...

Great package expss!!!!

from expss.

robertogilsaura avatar robertogilsaura commented on May 26, 2024

Hi @gdemin

I'm deploying the process in rmarkdown, but I didn't get print tables in expss_output_viewer() mode.

This is my chunk

suppressMessages(library(expss, quietly=TRUE))
data <- suppressMessages(read_spss("~/_datos/92.expss/ems2018.sav"))
options(scipen=5,width=255)
j <- val_lab(data$ZONA) 
for (i in sort(unique(data$ZONA)))
    {
data %>%
        tab_cells(P15) %>%
        tab_cols(total(), SEXO) %>%
        tab_subgroup(ZONA == i) %>% 
        tab_stat_cpct() %>%
        tab_cells(na_if(P15, gt(5))) %>% 
        tab_stat_mean_sd_n() %>%
        tab_pivot(stat_position = "inside_rows") %>% 
        set_caption(paste("|",attr(j,"names")[i])) %>% 
        print()
}

Table always are in text mode in the final html output, but I dont find the way for print in viewer mode. If I write expss_output_viewer() inside function, but output is HTML code.

Thank you in advance for your help ...

ems2018.zip

from expss.

gdemin avatar gdemin commented on May 26, 2024

Hmm... It seems it is rather tricky to print something with loops in the RMarkdown. Solution from here:

suppressMessages(library(expss, quietly=TRUE))
data <- suppressMessages(read_spss("~/_datos/92.expss/ems2018.sav"))
options(scipen=5,width=255)
j <- val_lab(data$ZONA) 
for (i in sort(unique(data$ZONA)))
    {
data %>%
        tab_cells(P15) %>%
        tab_cols(total(), SEXO) %>%
        tab_subgroup(ZONA == i) %>% 
        tab_stat_cpct() %>%
        tab_cells(na_if(P15, gt(5))) %>% 
        tab_stat_mean_sd_n() %>%
        tab_pivot(stat_position = "inside_rows") %>% 
        set_caption(paste("|",attr(j,"names")[i])) %>% 
        htmlTable() %>%
        print()
}

Note that you need to add results='asis' to your chunk options.

from expss.

robertogilsaura avatar robertogilsaura commented on May 26, 2024

​Hi @gdemin

Success, great !!!! runs properly... I would never have found the solution !!!

Here leave code for help someone with same problem... Note that is a chunk
{r, results='asis'}
j <- val_lab(data$D)
for (i in sort(unique(data$D)))
{
output <- data %>%
tab_cells(A) %>%
tab_weight(B) %>%
tab_cols(total(), C) %>%
tab_subgroup(D== i) %>%
tab_stat_cpct() %>%
tab_cells(na_if(A, gt(5))) %>%
tab_stat_mean_sd_n() %>%
tab_pivot(stat_position = "inside_rows") %>%
set_caption(attr(j,"names")[i])
print(htmlTable(output))
}

from expss.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.