Giter Site home page Giter Site logo

Comments (4)

gdemin avatar gdemin commented on June 23, 2024

I think it is better to make functions with two arguments - data and character variable name. And for selection use mrset_p - it selects by perl-style regular expression:

library(DT)
library(dplyr)
library(expss)
library(highcharter)

data <- data.frame(
    R1 = c(1, 2, 3, 4, 5, 6),
    R2 = c(2, 4, 6, 8, 10, 12),
    R3_1 = c(1, 1,3, 2, 1, 1),
    R3_2 = c(3, 2, 1, 1, 1, 2)
)


f1 <- function(data, variable){
    tab <- data %>% 
        tab_cols(total()) %>% 
        tab_cells('|'=unvr(data[[variable]])) %>% 
        tab_stat_cpct(total_row_position = 'none') %>% 
        tab_pivot() 
    colnames(tab) <- c('lab', 'pct')
    dt <- as.datatable_widget(tab)
    grf <- hchart(tab,'bar', hcaes(lab, pct))
    return(list(tab, dt, grf))
}

f1m <- function(data, variable){
    tab <- data %>% 
        tab_cols(total()) %>% 
        tab_cells('|'=unvr(mrset_p(variable))) %>% 
        tab_stat_cpct(total_row_position = 'none') %>% 
        tab_pivot() 
    colnames(tab) <- c('lab', 'pct')
    dt <- as.datatable_widget(tab)
    grf <- hchart(tab,'bar', hcaes(lab, pct))
    return(list(tab, dt, grf))
}

listR1 <- f1(data, "R1")
listR2 <- f1(data, "R2")
listR3 <- f1m(data, "^R3_") # "^" means "starts with"

from expss.

robertogilsaura avatar robertogilsaura commented on June 23, 2024

Thanks @gdemin. It runs ok. I save your recommendation.

Another question arose in this project. I have to create a header with new variables created from a condition of the type:

df=data.frame(A4_1=c(1,2,3,1,6,2,1), A4_2=c(6,6,8,3,4,5,6))
df %>%
    tab_cells(total()
        ,'Pregnancy1'=mrset(A4_1 %to% A4_2) %has% 6 
        ,'Pregnancy2'=mrset(A4_1 %to% A4_2) %has% 8
         ) %>%
    tab_stat_cases() %>% 
    tab_pivot()

The result for each line is TRUE/FALSE columns. Can we change label (TRUE/FALSE) of the columns in the table request? I tried using the label= parameter of mrset() but it had no effect on the output. I know I can edit the final etable object or create the variables initially, but in case there is a trick utility to request on the same table.

Thanks in advance.
Best regards.

from expss.

gdemin avatar gdemin commented on June 23, 2024

You can set value labels on the result:

library(expss)
df=data.frame(A4_1=c(1,2,3,1,6,2,1), A4_2=c(6,6,8,3,4,5,6))
df %>%
    tab_cells(total()
              ,'Pregnancy1'=mrset(A4_1 %to% A4_2) %has% 6 %>% set_val_lab(c("A" = TRUE, "B" = FALSE))
              ,'Pregnancy2'=mrset(A4_1 %to% A4_2) %has% 8 %>% set_val_lab(c("C" = TRUE, "D" = FALSE))
    ) %>%
    tab_stat_cases() %>% 
    tab_pivot()

from expss.

robertogilsaura avatar robertogilsaura commented on June 23, 2024

Wow, Awesome!!!

As always, I appreciate the work and quick your feedback.

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.