Giter Site home page Giter Site logo

Comments (6)

gdemin avatar gdemin commented on May 26, 2024

Hi, @robertogilsaura
I can't get the same result: sum(data$liters)/length(unique(data$hh)) gives me 5.92. What I am missing?
Generally speaking there is a function tab_stat_fun_df which can apply arbitrary function on several columns simultaneously.
We can construct function which will compute means on the base of unique the id:

mean_on_id = function(dfs){
    # dfs - data.frame
    # first column - value
    # all other columns - object id, it will be hh in our case
    # we should reference data.frame column by number because at runtime it will be unknown labels of the variables
    setNames(
        sum(dfs[[1]], na.rm = TRUE)/nrow(unique(dfs[,-1, drop = FALSE])),
        colnames(dfs)[1] # here we set name on the result
    )
}

Than we provide this function to the tab_stat_fun_df:

data%>%
    tab_cols (total (), gender) %>%
    tab_cells (data.frame(liters, hh))%>% # note the data.frame with two variables
    tab_stat_fun_df(mean_on_id) %>% 
    tab_pivot ()

Which give me:

 |        | #Total | gender |     |
 |        |        |      1 |   2 |
 | ------ | ------ | ------ | --- |
 | liters |    5.9 |    3.9 | 4.7 |

from expss.

robertogilsaura avatar robertogilsaura commented on May 26, 2024

from expss.

gdemin avatar gdemin commented on May 26, 2024

@robertogilsaura Excel attachment is missed

from expss.

robertogilsaura avatar robertogilsaura commented on May 26, 2024

Sorry I attached from gmail ..
Attach here the file ...
data.xlsx

Thanks

from expss.

gdemin avatar gdemin commented on May 26, 2024

As for your second case.
We create to column, one for households with males, other for hh with females. And then we use these columns as multiple response:

library(expss)

set.seed (311265) 
hh <- sample (1:75, 100, replace = TRUE) 
gender <- sample (1: 2, 100, replace = TRUE) 
liters <- sample (1: 5, 100, replace = TRUE) 
sc <- sample (1:10, 100, replace = TRUE) 
data <- data.frame (hh, gender, liters, sc) 



mean_on_id = function(dfs){
    # dfs - data.frame
    # first column - value
    # all other columns - object id, it will be hh in our case
    # we should reference data.frame column by number because at runtime it will be unknown labels of the variables
    setNames(
        sum(dfs[[1]], na.rm = TRUE)/nrow(unique(dfs[,-1, drop = FALSE])),
        colnames(dfs)[1] # here we set name on the result
    )
}

data%>%
    compute({
       # `ave` - function from base R - see ?ave. It is similar to window functions from SQL 
        gen_1 = ave(gender, hh, FUN = function(x) ifelse(any(x==1), 1, NA)) # hh has males
        gen_2 = ave(gender, hh, FUN = function(x) ifelse(any(x==2), 2, NA)) # hh has females
        var_lab(gen_1) = "Household type"
        val_lab(gen_1) = num_lab("
                                 1 Households with males
                                 2 Households with females
                                 ")
    }) %>% 
    tab_cols (total (), mrset(gen_1, gen_2)) %>%
    tab_cells (data.frame(liters, hh))%>% # note the data.frame with two variables
    tab_stat_fun_df(mean_on_id) %>% 
    tab_pivot ()

# |        | #Total |        Household type |                         |
# |        |        | Households with males | Households with females |
# | ------ | ------ | --------------------- | ----------------------- |
# | liters |  5.923 |                 6.853 |                   6.526 |

from expss.

robertogilsaura avatar robertogilsaura commented on May 26, 2024

Thanks @gdemin

Code runs properly. I will work with this pattern, because I have some combination for measures intragroup / between group. For instance ...

  • maxim intra / mean between
  • minim intra / mean between
  • sum intra / sum between
  • max intra / sum between
  • ...etc

.... among others

Thanks for your kindness and quick answers.

regards

Rober

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.