Home > Mobile >  Counting occurrences in R data frame with multiple filters
Counting occurrences in R data frame with multiple filters

Time:12-06

I have a table that looks like so:

Gender Time Payband
male part time £15,001-20000
male full time £25001-30000
female full time £35001-40000
male part time £35001-40000
female part time £35001-40000
female full time £25001-30000

And I need R code that makes 2 different dataframes that are filtered by 'Time' and give a count of the different genders in each payband. For example this table below would be filtered where time == part time:

Payband Male Female Total
£15001-20000 1 0 1
£20001-25000 0 0 0
£25001-30000 0 0 0
£35001-40000 1 1 2

There would also be a dataframe where time == full time

I imagine it would be a case of using things such as group_by and summarize but I just can't wrap my head around how to do it. Any help is greatly appreciated and I hope I am explaining the problem properly.

CodePudding user response:

You can do

pay <- c("£15,001-20000", "£20001-25000", "£25001-30000", "£35001-40000")

with(subset(df, Time == 'full time'), t(table(Gender, factor(Payband, pay)))) |>
  as.data.frame() |>
  tidyr::pivot_wider(names_from = 'Gender', values_from = 'Freq') |>
  dplyr::rename(Payband = Var1)
#> # A tibble: 4 x 3
#> Payband       female  male
#> <fct>          <int> <int>
#> 1 £15,001-20000      0     0
#> 2 £20001-25000       0     0
#> 3 £25001-30000       1     1
#> 4 £35001-40000       1     0

with(subset(df, Time == 'part time'), t(table(Gender, factor(Payband, pay)))) |>
  as.data.frame() |>
  tidyr::pivot_wider(names_from = 'Gender', values_from = 'Freq') |>
  dplyr::rename(Payband = Var1)
#> # A tibble: 4 x 3
#>  Payband       female  male
#>  <fct>          <int> <int>
#> 1 £15,001-20000      0     1
#> 2 £20001-25000       0     0
#> 3 £25001-30000       0     0
#> 4 £35001-40000       1     1
  • Related