2

I would like to use a string as argument of a function in order to use this string for the plotting of the result, but R plots the argument variable name instead of its string value. I tried different solutions (diparse, as.character...) but still no solution. Do you have any idea?

mcnemar_test <- function (c1,c2,class1, class2)
{
    name1=label(class1)
    name2=deparse(substitute(class2))

    v1 = c1$encerts
    v2 = c2$encerts

    e00 = sum(ifelse(v1+v2==0,1,0)) #bad classification for both
    e01 = sum(ifelse(v1<v2,1,0)) #bad classification for 1
    e10 = sum(ifelse(v1>v2,1,0)) #bad classification for 2
    e11 = sum(ifelse(v1+v2==2,1,0)) #good classification for both

    matriu <- matrix(c(e00,e01,e10,e11),nrow = 2, 
        dimnames = list(name1 = c("Disapprove", "Approve"),
                       name2 = c("Disapprove", "Approve")))
    print (matriu)
    t <- mcnemar.test(matriu)
    return (t)
}
mcnemar_test(classifiers.NaiveBayes,classifiers.CART,"aa","bb")

I would like to see "aa" and "bb" but see "name1 and name2

Gornitorink
  • 127
  • 2
  • 6
  • Have you tried `eval`? Lots of Q&A here on this kind of thing – Ben Jun 01 '13 at 16:24
  • @Ben eval is just one letter away from R inferno; best avoided when not really needed – baptiste Jun 01 '13 at 18:28
  • Quite right, there are less obscure methods, it is [slow](https://stat.ethz.ch/pipermail/r-help/2007-January/123208.html), indicative of a [lack of knowledge](http://stackoverflow.com/q/13649979/1036500) and there are many [alternatives](http://www.talkstats.com/showthread.php/20974-Why-is-eval-parse-bad?p=67452&viewfull=1#post67452), such as [get](http://stackoverflow.com/questions/13647046/avoiding-the-infamous-evalparse-construct), and [`[[`](http://stackoverflow.com/a/11025440/1036500). What is the one letter away that you're referring to (I'm not _that_ familiar with that text...) – Ben Jun 01 '13 at 22:34

2 Answers2

2

R thinks you want the names to be "name1" and "name2", just like if I were to create a list with names "a" and "b":

my.list <- list(a=1, b=2)

Try using structure and passing the names as a character vector:

matriu <- matrix(c(e00,e01,e10,e11),nrow = 2, 
                 dimnames = structure(list(c("Disapprove", "Approve"),
                                           c("Disapprove", "Approve")),
                                      names=c(class1, class2)))

Or setting the names of the elements after you create the list:

matriu <- matrix(c(e00,e01,e10,e11),nrow = 2, 
                 dimnames = list(c("Disapprove", "Approve"),
                                 c("Disapprove", "Approve")))
names(dimnames(matriu)) <- c(class1, class2)
Peyton
  • 7,266
  • 2
  • 29
  • 29
-1

Edit: Within your function code drop the label(.) and deparse(substitute(.)) attempts and use this:

dimnames = setNames( list(  c("Disapprove", "Approve"),  
                            c("Disapprove", "Approve")), 
                     c(class1, class2) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487