In a custom R function that calls ezANOVA: How do I parameterize the dv?

TSeymour

I'm trying to use ezANOVA from the ez package within a function where I want to allow the dv to be specified using a parameter. Normally, ezANOVA will accept the column variable as a symbol or character string (see "This Works" below). However, trying to give ezANOVA a parameter that holds a symbol or character doesn't work (see "This Does Not Work" below). ezANOVA complains that '"the_dv" is not a variable in the data frame provided'. I've tried wrapping the variable name in various methods like as.symbol(), as.formula(), and even tried various ways to incorporate eval() and substitute(), but all with no luck. How is this achieved?

If the why of it helps, i have an project where I need to do many compound analyses (means, anovas, post-hocs, graphs) that are identical expect for the dataset or the variable being analyzed. I want a function so I can write it once and run it many times. The code below is just a simple example.

library(ez)

df<-data.frame(ID=as.factor(101:120), 
               Training=rep(c("Jedi", "Sith"), 10), 
               Wins=sample(1:50, 20), 
               Losses=sample(1:50, 20))

# ----------
# This Works
# ----------

myfunc1 <- function(the_data) {
  ezANOVA(
    data = the_data,
    wid = ID,
    dv = Wins,
    between = Training
  )
}

myfunc1(the_data = df)

# ------------------
# This Does Not Work
# -------------------

myfunc2 <- function(the_data, the_dv) {
  ezANOVA(
    data = the_data,
    wid = ID,
    dv = the_dv,
    between = Training
  )
}

myfunc2(the_data = df, the_dv = Wins)  # 'Wins' also fails
TSeymour

Had to solve this one myself. Turns out that a combination of eval() and substitute() solves this puzzle:

# ----------------------------------
# Aha, it works!
# ----------------------------------

library(ez)

df<-data.frame(ID=as.factor(101:120), 
               Training=rep(c("Jedi", "Sith"), 10), 
               Wins=sample(1:50, 20), 
               Losses=sample(1:50, 20))

myfunc2 <- function(the_data, the_dv) {
  eval(
    substitute(
      ezANOVA(data = the_data, 
              wid = ID, 
              dv = the_dv, 
              between = Training), 
      list(the_dv = the_dv)))
}

myfunc2(the_data = df, the_dv = 'Wins')
myfunc2(the_data = df, the_dv = 'Losses')

Enjoy!!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can I parameterize select function in scandir

From Dev

How do I pass constant parameters through an optimization function that calls another function in R?

From Dev

How do I save the contents of ellipsis in R to be used in deeply nested function calls?

From Dev

In Shiny, how do I directly substitute source code into R function calls or statements?

From Dev

How do I graph a custom function in OxyPlot?

From Dev

How do I parameterize my Class parameter in this case?

From Dev

How do I parameterize the baseUrl property of the protractor config file

From Dev

How do I efficiently trace nested function calls in C?

From Dev

How do I find calls to a function in Git from another file?

From Dev

How do I put Utilities.sleep() between function calls?

From Dev

How do I reuse a test function with async calls in nodejs?

From Dev

How do I find calls to a function in Git from another file?

From Dev

How do I use the dot syntax in Julia function calls

From Dev

How do I retrieve the processor time in Linux without function calls?

From Dev

How do I apply a custom function with multiple data.frames as input in R?

From Dev

Can I use child process or cluster to do custom function calls in node?

From Java

How do I make this function in R?

From Dev

How do I understand this R function?

From Dev

How do I verify that a function is a PDF in R?

From Dev

How do I test a function that calls an AJAX function without firing it using jasmine?

From Dev

How do I use recursion to write a function that calls another function repeatedly in Python?

From Dev

How do I do consecutive function calls using the completion handler in Swift on an SKSpriteNode?

From Dev

How do I do consecutive function calls using the completion handler in Swift on an SKSpriteNode?

From Dev

Parameterize pattern match as function argument in R

From Dev

How do I disable the touchpad using the upper left corner on an HP Pavilion dv6?

From Dev

How do I disable the touchpad using the upper left corner on an HP Pavilion dv6?

From Dev

How do I use a custom comparator function with BTreeSet?

From Dev

How do I pass variables to a custom function in ddply?

From Dev

How do I specify a custom filter function for ng-repeat?

Related Related

  1. 1

    how can I parameterize select function in scandir

  2. 2

    How do I pass constant parameters through an optimization function that calls another function in R?

  3. 3

    How do I save the contents of ellipsis in R to be used in deeply nested function calls?

  4. 4

    In Shiny, how do I directly substitute source code into R function calls or statements?

  5. 5

    How do I graph a custom function in OxyPlot?

  6. 6

    How do I parameterize my Class parameter in this case?

  7. 7

    How do I parameterize the baseUrl property of the protractor config file

  8. 8

    How do I efficiently trace nested function calls in C?

  9. 9

    How do I find calls to a function in Git from another file?

  10. 10

    How do I put Utilities.sleep() between function calls?

  11. 11

    How do I reuse a test function with async calls in nodejs?

  12. 12

    How do I find calls to a function in Git from another file?

  13. 13

    How do I use the dot syntax in Julia function calls

  14. 14

    How do I retrieve the processor time in Linux without function calls?

  15. 15

    How do I apply a custom function with multiple data.frames as input in R?

  16. 16

    Can I use child process or cluster to do custom function calls in node?

  17. 17

    How do I make this function in R?

  18. 18

    How do I understand this R function?

  19. 19

    How do I verify that a function is a PDF in R?

  20. 20

    How do I test a function that calls an AJAX function without firing it using jasmine?

  21. 21

    How do I use recursion to write a function that calls another function repeatedly in Python?

  22. 22

    How do I do consecutive function calls using the completion handler in Swift on an SKSpriteNode?

  23. 23

    How do I do consecutive function calls using the completion handler in Swift on an SKSpriteNode?

  24. 24

    Parameterize pattern match as function argument in R

  25. 25

    How do I disable the touchpad using the upper left corner on an HP Pavilion dv6?

  26. 26

    How do I disable the touchpad using the upper left corner on an HP Pavilion dv6?

  27. 27

    How do I use a custom comparator function with BTreeSet?

  28. 28

    How do I pass variables to a custom function in ddply?

  29. 29

    How do I specify a custom filter function for ng-repeat?

HotTag

Archive