将字符串作为函数参数传递

时间:2013-06-01 15:55:59

标签: r

我想使用字符串作为函数的参数,以便使用此字符串来绘制结果,但是R绘制参数变量名而不是字符串值。 我尝试了不同的解决方案(diparse,as.character ......)但仍然没有解决方案。你有什么想法吗?

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")

我想看“aa”和“bb”,但请参阅“name1和name2

2 个答案:

答案 0 :(得分:2)

R认为您希望名称为“name1”和“name2”,就像我要创建名为“a”和“b”的列表一样:

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

尝试使用structure并将名称作为字符向量传递:

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

或者在创建列表后设置元素的名称:

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

答案 1 :(得分:-1)

编辑:在你的功能代码中删除标签(。)和deparse(替换(。))尝试并使用它:

dimnames = setNames( list(  c("Disapprove", "Approve"),  
                            c("Disapprove", "Approve")), 
                     c(class1, class2) )