<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/descriptive-summary-proportions-of-values-in-a-vector-rstats/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://strengejacke.wordpress.com/2017/03/06/descriptive-summary-proportions-of-values-in-a-vector-rstats/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>When describing a sample, researchers in my field often show proportions of specific characteristics as description. For instance, proportion of female persons, proportion of persons with higher or lower income etc. Since it happens often that I like to know these characteristics when exploring data, I decided to write a function, <code>prop()</code>, which is part of my <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sjstats">sjstats</a>-package – a package dedicated to summary-functions, mostly for fit- or association-measures of regression models or descriptive statistics.</p>
<p><code>prop()</code> is designed following a similar fashion like <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/2017/02/08/data-wrangling-within-the-tidyverse-the-design-philosophy-behind-the-sjmisc-package-rstats/">most functions of my sjmisc-package</a>: first, the data; then an user-defined number of logical comparisons that define the proportions. A single comparison argument as input returns a vector, multiple comparisons return a tibble (where the first column contains the comparison, and the second the related proportion).</p>
<p>An examle from the <em>mtcars</em> dataset:</p>
<pre>library(sjstats)
data(mtcars)
# proportions of observations in mpg that are greater than 25
prop(mtcars, mpg > 25)
#> [1] 0.1875

prop(mtcars, mpg > 25, disp > 200, gear == 4)
#> # A tibble: 3 × 2
#>   condition   prop
#>       &lt;chr>  &lt;dbl>
#> 1    mpg>25 0.1875
#> 2  disp>200 0.5000
#> 3   gear==4 0.3750</pre>
<p>The function also works on grouped data frames, and with <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/sjmisc/vignettes/intro_sjmisc.html">labelled data</a>. In the following example, we group a dataset on family carers by their gender and education, and then get the proportions of observations where care-receivers are at least moderately dependent and male persons. To get an impression of how the raw variables look like, we first compute simple frequency tables with <code>frq()</code>.</p>
<pre>library(sjmisc) # for frq()-function
data(efc)
frq(efc, e42dep)
#> # elder's dependency
#> 
#>  val                label frq raw.prc valid.prc cum.prc
#>    1          independent  66    7.27      7.33    7.33
#>    2   slightly dependent 225   24.78     24.97   32.30
#>    3 moderately dependent 306   33.70     33.96   66.26
#>    4   severely dependent 304   33.48     33.74  100.00
#>    5                   NA   7    0.77        NA      NA

frq(efc, e16sex)
#> # elder's gender
#> 
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 296   32.60     32.85   32.85
#>    2 female 605   66.63     67.15  100.00
#>    3     NA   7    0.77        NA      NA

efc %>%
  select(e42dep, c161sex, c172code, e16sex) %>%
  group_by(c161sex, c172code) %>%
  prop(e42dep > 2, e16sex == 1)

#> # A tibble: 6 × 4
#>   `carer's gender`    `carer's level of education` `e42dep>2` `e16sex==1`
#>              &lt;chr>                           &lt;chr>      &lt;dbl>       &lt;dbl>
#> 1             Male          low level of education     0.6829      0.3659
#> 2             Male intermediate level of education     0.6590      0.3155
#> 3             Male         high level of education     0.7872      0.2766
#> 4           Female          low level of education     0.7101      0.4638
#> 5           Female intermediate level of education     0.5929      0.2832
#> 6           Female         high level of education     0.6881      0.2752</pre>
<p>So, within the group of male family carers with low level of education, 68.29% of care-receivers are moderately or severely dependent, and 36.59% of care-receivers are male. Within female family carers with high level of education, 68.81% of care-receivers are at least moderately dependent and 27.52% are male.</p>
<p> Tagged: <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/r/">R</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/rstats/">rstats</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/strengejacke.wordpress.com/1251/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/strengejacke.wordpress.com/1251/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://strengejacke.wordpress.com/2017/03/06/descriptive-summary-proportions-of-values-in-a-vector-rstats/"> R – Strenge Jacke!</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/pcerPlnWkoM"></a></div>