简体   繁体   中英

Deparse.level = 2 in R

I need to know what deparse.level = 2 means and what it does to a table and everything I write makes me more confused. Can anyone help me please?

Thank you

I tried to apply the table without diparse.level and I can see the order of the table changes but it also adds labels so I cant undersdant what exactly it is meant to do

Not sure what you mean with the ordering part, but table(..., deparse.level = 2) makes the function willing to name the dimensions in the table things that are not symbols (variable names, basically) if an argument wasn't named in the call. In effect it tries extra hard to assign names to the dimensions even if they're something like a function call, for example. See the Details and Examples in the next help text with ?table .


More detail:

The documentation technically does explain it, but it's... a bit dense:

If the argument dnn is not supplied, the internal function list.names is called to compute the 'dimname names'. If the arguments in... are named, those names are used. For the remaining arguments, deparse.level = 0 gives an empty name, deparse.level = 1 uses the supplied argument if it is a symbol, and deparse.level = 2 will deparse the argument.

There's a good example below that though:

> a <- letters[1:3]
> table(a, sample(a))                    # dnn is c("a", "")
   
a   a b c
  a 0 0 1
  b 1 0 0
  c 0 1 0
> table(a, sample(a), deparse.level = 0) # dnn is c("", "")
   
    a b c
  a 1 0 0
  b 0 0 1
  c 0 1 0
> table(a, sample(a), deparse.level = 2) # dnn is c("a", "sample(a)")

   sample(a)
a   a b c
  a 1 0 0
  b 0 0 1
  c 0 1 0

Only in the last one is it willing to name a dimension "sample(a)". In all those cases the second vector isn't given as a named argument, so it tries to figure out what symbol to use for it (with level 1, the default) or what text of any kind to use for it (with level 2).


Even more:

And about what it means by "if it is a symbol," see ?is.symbol and ?deparse and the rabbit hole that leads to. It's not about how weird the name looks; you can do something like this, and it's fine with it at deparse level 1 since it is a symbol in this context:

> `sample(a)` <- sample(a)
> table(a, `sample(a)`)
   sample(a)
a   a b c
  a 0 0 1
  b 1 0 0
  c 0 1 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM