R: Use column name of df as input for custom function

65 views Asked by At

I am trying to make a custom function that takes a df and the name of the column I want to apply the function to. however I am having trouble calling my function.

When I enter the name of the column without parentheses, I get "Error: object 'Area' not found". When I enter the column name as a character, I get "Error in model.frame.default(form, data) : variable lengths differ (found for 'Treatment')".

I am guessing I need to re-characterize my column name inside the function, but I don't know what to do.

AssumTests <- function(data, aspect){
  # Homogeneity of Variance test
  lt <- leveneTest(aspect ~ Treatment, 
             data = data)
  
  model  <- lm(aspect ~ Treatment, data = data)
  
  # Normality residuals as group
  qq <- ggqqplot(residuals(model))
  st <- shapiro_test(residuals(model))
  
  # Normality test by group
  qqfac <- ggqqplot(data, paste(aspect), facet.by = "Treatment")
  spfac <- data %>%
    group_by(Treatment) %>%
    shapiro_test(aspect)
  
  list = list(lt, qq, st, qqfac, spfac)
  return(list)
}
AssumTests(FluorByTreatment_Iter1.df, "Area")

Crossposted to: Reddit

UPDATE: Got the first expressions to work with data[[aspect]], but still having trouble with the last expression using dplyr piping. I tried the {{aspect}} trick, but R is still recognizing the quotes: "Column "Area" doesn't exist."

UPDATE2: Got the final expression to work, thanks to thisReddit comment using !!sym(column)

1

There are 1 answers

0
Nathan Stutzman On

Got the first 3 expressions to work with data[[aspect]], and got the final expression to work, using !!sym(column)

So final code being:

AssumTests <- function(data, aspect){
  # Homogeneity of Variance test
  lt <- leveneTest(data[[aspect]] ~ Treatment, 
             data = data)
  
  model  <- lm(data[[aspect]] ~ Treatment, data = data)
  
  # Normality residuals as group
  qq <- ggqqplot(residuals(model))
  st <- shapiro_test(residuals(model))
  
  # Normality test by group
  qqfac <- ggqqplot(data, aspect, facet.by = "Treatment")
  spfac <- data %>%
    group_by(Treatment) %>%
    shapiro_test(!!sym(aspect))
  
  list = list(lt, qq, st, qqfac, spfac)
  return(list)