Comments (6)
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.
from expss.
@robertogilsaura Excel attachment is missed
from expss.
Sorry I attached from gmail ..
Attach here the file ...
data.xlsx
Thanks
from expss.
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.
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)
- Mismatches between significance test result of R and SPSS HOT 6
- How to not include subtotals in percent calculation? HOT 4
- Redundant column when using subtotal in columns HOT 4
- Help with val_lab() and var_lab() HOT 3
- Intermittent error: "duplicated values in labels" HOT 5
- Converting etable to huxtable leaves duplicate cells empty HOT 2
- 'do_repeat' functionality HOT 1
- Random error 'set_val_lab' - duplicated values in labels: HOT 13
- Please keep do_repeat HOT 2
- Unable to install without 'knitr' - move to Depends? HOT 1
- Problem at writting labelled csv files with accents in labels HOT 10
- Question about computing percentages with different bases HOT 3
- What happened to vlookup? HOT 1
- Where deprecated in future HOT 2
- Question about net HOT 2
- Shiny and expss HOT 2
- var_lab in net HOT 2
- Multiple response in function() HOT 4
- expss--where did it go? HOT 1
- use of `base::mtfrm` entails R 4.2 or later HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expss.