3

I want to create a custom function to calculate grouped percentages in a large dataset with 100+ columns. Because I have so many columns I want to do a loop or lapply or something to avoid typing the function out 100+ times. The function I wrote works fine when I type it in individually for each column, but I cannot figure out how to do it repeatedly.

Here's a simplified dataframe and function:

# load required libraries:
library(tidyverse)

df<-data.frame(sex=c('M','M','M','F','M','F','M',NA),
              school=c('A','A','A','A','B','B','B',NA),
              question1=c(NA,1,1,2,2,3,3,3),
              question2=c(2,NA,2,4,5,1,2,3))

 my_function<-function(dataset,question_number){

  question_number_enquo<-enquo(question_number)

  dataset%>%
    filter(!is.na(!!question_number_enquo)&!is.na(sex))%>%
    group_by(school,sex,!!question_number_enquo)%>%
    count(!!question_number_enquo)%>%
    summarise(number=sum(n))%>%
    mutate(percent=number/sum(number)*100)%>%
    ungroup()
}

My function works when I type a column name into it:

my_function(df,question1)

 A tibble: 5 x 5
  school sex   question1 number percent
  <fct>  <fct>     <dbl>  <int>   <dbl>
1 A      F             2      1     100
2 A      M             1      2     100
3 B      F             3      1     100
4 B      M             2      1      50
5 B      M             3      1      50

Here's what I've tried in terms of reiteration. I want to repeat the function for every column (except for school and sex, because those are my groups).

question_col_names<-(df%>%select(-sex,-school)%>%colnames())

Using lapply with the column names as a quosure:

question_col_names_enquo<-enquo(question_col_names)
lapply(df,my_function(df,!!question_col_names_enquo))


 Error: Column `<chr>` must be length 7 (the number of rows) or one, not 2

Trying lapply with unquoted column names:

lapply(df,my_function(df,question_col_names))

Error: Column `question_col_names` is unknown

Trying lapply with quoted column names:

lapply(df,my_function(df,'question_col_names'))

Error: Column `"question_col_names"` can't be modified because it's a grouping variable

I also tried apply, and got the same types of error messages:

apply(df,1,my_function(df,!!question_col_names_enquo))
Error: Column `<chr>` must be length 7 (the number of rows) or one, not 2

apply(df,1,my_function(df,question_col_names))
Error: Column `question_col_names` is unknown

apply(df,1,my_function(df,'question_col_names'))
Error: Column `"question_col_names"` can't be modified because it's a grouping variable

I also tried different variations of a for loop:

for (i in question_col_names){
  my_function(df,i)
}

Error: Column `i` is unknown


for (i in question_col_names){
   my_function(df,'i')
 }
Error: Column `"i"` can't be modified because it's a grouping variable

How can I use iteration to get my function to repeat over all my columns?

I suspect that this has to do with dplyr; I know that it acts funny in custom functions, but I can get it to work in my function, just not in the iteration. I've done a deep dive on Google and Stack Overflow but haven't found anything that answered this.

Thanks in advance!

Tung
  • 26,371
  • 7
  • 91
  • 115
kellyd
  • 33
  • 5

2 Answers2

4

Your question_col_names are strings. You need sym to convert string to variable inside your function instead

library(tidyverse)

df <- data.frame(
  sex = c("M", "M", "M", "F", "M", "F", "M", NA),
  school = c("A", "A", "A", "A", "B", "B", "B", NA),
  question1 = c(NA, 1, 1, 2, 2, 3, 3, 3),
  question2 = c(2, NA, 2, 4, 5, 1, 2, 3)
)

my_function <- function(dataset, question_number) {
  question_number_enquo <- sym(question_number)

  dataset %>%
    filter(!is.na(!!question_number_enquo) & !is.na(sex)) %>%
    group_by(school, sex, !!question_number_enquo) %>%
    count(!!question_number_enquo) %>%
    summarise(number = sum(n)) %>%
    mutate(percent = number / sum(number) * 100) %>%
    ungroup()
}

my_function(df, "question1")
#> # A tibble: 5 x 5
#>   school sex   question1 number percent
#>   <fct>  <fct>     <dbl>  <int>   <dbl>
#> 1 A      F             2      1     100
#> 2 A      M             1      2     100
#> 3 B      F             3      1     100
#> 4 B      M             2      1      50
#> 5 B      M             3      1      50

question_col_names <- (df %>% select(-sex, -school) %>% colnames())

result <- map_df(question_col_names, ~ my_function(df, .x))
result
#> # A tibble: 10 x 6
#>    school sex   question1 number percent question2
#>    <fct>  <fct>     <dbl>  <int>   <dbl>     <dbl>
#>  1 A      F             2      1     100        NA
#>  2 A      M             1      2     100        NA
#>  3 B      F             3      1     100        NA
#>  4 B      M             2      1      50        NA
#>  5 B      M             3      1      50        NA
#>  6 A      F            NA      1     100         4
#>  7 A      M            NA      2     100         2
#>  8 B      F            NA      1     100         1
#>  9 B      M            NA      1      50         2
#> 10 B      M            NA      1      50         5

Probably better if you convert your function result to long format

my_function2 <- function(dataset, question_number) {
  question_number_enquo <- sym(question_number)

  res <- dataset %>%
    filter(!is.na(!!question_number_enquo) & !is.na(sex)) %>%
    group_by(school, sex, !!question_number_enquo) %>%
    count(!!question_number_enquo) %>%
    summarise(number = sum(n)) %>%
    mutate(percent = number / sum(number) * 100) %>%
    ungroup() %>% 
    gather(key = 'question', value, -school, -sex, -number, -percent)
  return(res)

}

result2 <- map_df(question_col_names, ~ my_function2(df, .x))
result2
#> # A tibble: 10 x 6
#>    school sex   number percent question  value
#>    <fct>  <fct>  <int>   <dbl> <chr>     <dbl>
#>  1 A      F          1     100 question1     2
#>  2 A      M          2     100 question1     1
#>  3 B      F          1     100 question1     3
#>  4 B      M          1      50 question1     2
#>  5 B      M          1      50 question1     3
#>  6 A      F          1     100 question2     4
#>  7 A      M          2     100 question2     2
#>  8 B      F          1     100 question2     1
#>  9 B      M          1      50 question2     2
#> 10 B      M          1      50 question2     5

Created on 2019-11-25 by the reprex package (v0.3.0)

Tung
  • 26,371
  • 7
  • 91
  • 115
2

If I understood correctly, you can use gather, nest and map for this:

library(tidyverse)

df %>% 
  rownames_to_column("ID") %>% 
  gather(question, value, -ID, -sex, -school) %>% 
  nest(-sex, -school) %>% 
  mutate(results = purrr::map(data, function(x) { 
    x %>% 
      group_by(question)%>%
      summarise(number=sum(!is.na(value))) %>%
      mutate(percent=number/sum(number)*100)%>%
      ungroup()})) %>% 
  select(sex, school, results) %>%
  unnest(results) 

Results:

   sex   school question  number percent
   <fct> <fct>  <chr>      <int>   <dbl>
 1 M     A      question1      3      50
 2 M     A      question2      3      50
 3 F     A      question1      1      50
 4 F     A      question2      1      50
 5 M     B      question1      2      50
 6 M     B      question2      2      50
 7 F     B      question1      1      50
 8 F     B      question2      1      50
 9 NA    NA     question1      1      50
10 NA    NA     question2      1      50
DJV
  • 4,743
  • 3
  • 19
  • 34
  • I'm looking to see the percent of people in each sex/school group that had each answer.. I modified your code so the 'percent' column referred to the percent of each answer in that group: df%>%gather(question,value,-sex,-school)%>% group_by(question,sex,school)%>% nest()%>% mutate(results=map(data,function(x){ x%>%filter(!is.na(value))%>%count(value)%>% mutate(percent=n/sum(n)*100)%>%ungroup()}))%>%unnest(results) Thank you! I hadn't done much with nesting so that was helpful. – kellyd Nov 25 '19 at 18:15
  • What's the importance of adding the rownames_to_column? I get the same results either way. – kellyd Nov 25 '19 at 18:20
  • You're welcome and glad I could help. Note that you don't need to `group_by()` and then `nest()`, you should `nest() you suppose to `nest` your `group_by` vars. I used `rownames_to_column` to create a key/ID for each row. When you `gather()` or `spread` you need a key so R will know which row belongs to which participant. Try to look here for more info about the `tidyverse` amazing world :) - https://www.tidyverse.org – DJV Nov 26 '19 at 08:44