4

The following function creates a table with the variable names as dimension names

col.table <- function(var1, var2, C=T,weights=rep(1,length(var1)), margins=TRUE,data,env=parent.frame()){ 
  require(weights); require(Hmisc)
  v1 <- deparse(substitute(var1)) 
  v2 <- deparse(substitute(var2)) 
  if(!missing(data)){
    var1 <- data[,deparse(substitute(var1))]
    var2 <- data[,deparse(substitute(var2))]
    weights <- data[,deparse(substitute(weights))]
  }


  if (C) {
    crosstab <-prop.table(xtabs(weights ~ var1 + var2,data), margin=2)

    t <- cbind(crosstab, Total=prop.table(xtabs(weights ~ var1,data=data)))
    t <- rbind(t,Total = colSums(t))
    bu<-c(deparse(substitute(v1)), deparse(substitute(v2)))

    names(dimnames(t)) <- bu
    return(round(100*t,2))

}}

Some dummy data

d<-data.frame(
  vara =c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
  varb = c(1,1,2,2,3,3,1,1,2,2,3,3,1,1,2),
  varc= c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
  weight= c(.5,.5,.5,.5,.5,1,1,1,1,1,2,2,2,2,2))

a<-col.table(vara,varb,data=d,weights=weight)
a

I'd like the returned object (a) to show the variable names without the quotes (just vara and varb instead of "vara" and "varb" in this case). Does anyone know how to do this? I want to remove the quotes within the function rather than outside it.

4

4 回答 4

5

You did something that you did not intend, leading to this behavior.

v1 and v2 are not function arguments (i.e., promises). They are character variables, in the function's local environment. Passing them to substitute does nothing (as they are not promises), but passing to deparse adds quote characters on each side of the value. This is the problem with your code.

First:

v1 <- deparse(substitute(var1)) 
v2 <- deparse(substitute(var2)) 

Then:

bu<-c(deparse(substitute(v1)), deparse(substitute(v2)))

Change this latter line to:

bu <- c(v1, v2)

This fixes the root of the problem, rather than masking it by removing the quote characters later.

Note the added quote characters in the following expression. This is what is going wrong with your function:

> deparse('abc')
[1] "\"abc\""
于 2012-12-28T02:16:22.547 回答
2

Use gsub('"','',...) to remove quotation marks: changing the relevant line of code to

names(dimnames(t)) <- gsub('"','',bu)

should do it, I think.

于 2012-12-27T22:16:30.587 回答
1

you coud do this:

 names(dimnames(a)) <- c("vara","varb")
于 2012-12-27T21:57:51.267 回答
1

Try as.name() to unquote a string whether with single or double quotes. If the string has an embedded space, as.name() returns it enclosed in backquotes (string) and it will be difficult to use it as a variable name, but maybe there is a way.

A demo:

> as.name("v7")
v7
> as.name('v7')

> as.name("v7 xx")
`v7 xx`
> as.name('v7 xx')
`v7 xx`
> as.name(`v7`)
Error in as.name(v7) : object 'v7' not found
> `v7` <= data.frame()
Error: object 'v7' not found
> 'v7' <= data.frame
Error in "v7" <= data.frame : 
  comparison (4) is possible only for atomic and list types
> "v7" <= data.frame
Error in "v7" <= data.frame : 
  comparison (4) is possible only for atomic and list types
于 2015-04-21T04:44:33.150 回答