Nick
Nick

Reputation: 859

Function not evaluating variables in an expected manner

I'm trying to write an R function to produce a frequency table so I can standardise formatting etc without typing it out repeatedly. The only problem is that I can't get it to evaluate a grouping variable correctly.

Here is some code to get a mini dataset to reproduce the problem:

 library(tidyverse)
 id <- sample(1:500, 5)
 factors <- sample(1:3, 5, replace = TRUE)
 data <- data.frame(id, factors)
 freqTable <- function(x, field){

     Table <- x %>%
         group_by(field) %>%
         summarise(N = n(), Percent = n()/NROW(x)*100) %>%
         mutate(C.Percent = cumsum(Percent))
     return(Table)
 }
 freqTable(data, "factors")

Which results in:

Error in resolve_vars(new_groups, tbl_vars(.data)) : unknown variable to group by : field Called from: resolve_vars(new_groups, tbl_vars(.data))

I've also tried:

freqTable <- function(x, field){
     Table <- x %>%
            group_by(paste(field)) %>%
            summarise(N = n(), Percent = n()/NROW(x)*100) %>%
            mutate(C.Percent = cumsum(Percent))
  return(Table)
}

Which works a little better (in that it doesn't error), but still doesn't actually group the factors correctly, outputting this:

# A tibble: 1 × 4
  `paste(field)`     N Percent C.Percent
           <chr> <int>   <dbl>     <dbl>
1        factors     5     100       100

Where it just tells me the number of cases in that column. Does anyone know where I'm going wrong here?

Upvotes: 1

Views: 199

Answers (1)

Nick
Nick

Reputation: 859

Sorry - just figured this one out.

group_by_(field)

I thought it might have something to do with non-standard evaluation, but I'm not too knowledgeable about it yet.

This:

freqTable <- function(x, field){
+      Table <- x %>%
+             group_by_(paste(field)) %>%
+             summarise(N = n(), Percent = n()/NROW(x)*100) %>%
+             mutate(C.Percent = cumsum(Percent))
+   return(Table)
+ }

Now gives this:

> freqTable(data, "factors")
# A tibble: 2 × 4
  factors     N Percent C.Percent
    <int> <int>   <dbl>     <dbl>
1       2     2      40        40
2       3     3      60       100

Upvotes: 1

Related Questions