Function table(x, y) 兼容 R 基础语言和本地管道。

huangapple go评论94阅读模式
英文:

Function table(x, y) compatible with R base language and native pipe

问题

以下是要翻译的内容:

I am looking to develop a tab(x, y) or tab(d, x, y) function compatible with r base language (table(d$x, d$y)) and native pipe (d |> table(x, y)).

Native pipe compatible function:

  1. tab1 <- function(d, x, y){
  2. eval(substitute(table(d$x, d$y)))
  3. }
  4. mtcars |&gt; tab1(cyl, vs)
  5. # or
  6. tab1(mtcars, cyl, vs)

R base:

  1. tab2 <- function (x, y) {
  2. result <- table(x, y)
  3. result
  4. }
  5. tab2(mtcars$cyl, mtcars$vs)

What should I add to the tab1 function so that the command tab1(mtcars, mtcars$cyl, mtcars$vs) does not return an error message (sometimes, using a pipe compatible function, the name of the dataframe is repeated incorrectly)?

I would like to get a function (tab(d, x, y) or tab(x, y)) that works with these commands:

  1. tab(mtcars, cyl, vs) # it's tab1 function
  2. mtcars |&gt; tab(cyl, vs) # it's tab1 function
  3. tab(mtcars, mtcars$cyl, mtcars$vs) # doesn't work with tab1 function

and ideally also with:

tab(mtcars$cyl, mtcars$vs) # it's tab2 function

英文:

I am looking to develop a tab(x, y) or tab(d, x, y) function compatible with r base language (table(d$x, d$y)) and native pipe (d |&gt; table(x, y)).

Native pipe compatible function:

  1. tab1 &lt;- function(d, x, y){
  2. eval(substitute(table(d$x, d$y)))
  3. }
  4. mtcars |&gt; tab1(cyl, vs)
  5. # or
  6. tab1(mtcars, cyl, vs)

R base:

  1. tab2 &lt;- function (x, y) {
  2. result &lt;- table(x, y)
  3. result
  4. }
  5. tab2(mtcars$cyl, mtcars$vs)

What should I add to the tab1 function so that the command tab1(mtcars, mtcars$cyl, mtcars$vs) does not return an error message (sometimes, using a pipe compatible function, the name of the dataframe is repeated incorrectly)?

I would like to get a function (tab(d, x, y) or tab(x, y)) that works with these commands:

  1. tab(mtcars, cyl, vs) # it&#39;s tab1 function
  2. mtcars |&gt; tab(cyl, vs) # it&#39;s tab1 function
  3. tab(mtcars, mtcars$cyl, mtcars$vs) # doesn&#39;t work with tab1 function

and ideally also with:

tab(mtcars$cyl, mtcars$vs) # it&#39;s tab2 function

答案1

得分: 2

使用这个定义,下面的示例都与table中的工作方式相同。请注意,table(mtcars$vs, mtcars$cyl)不显示名称,因此在这些情况下,tab3也不会显示名称。 (如果tab3中的table被替换为collapse中的qtab,它将显示名称。)

  1. tab3 <- function(d = parent.frame(), x, y, ...) {
  2. if (missing(y)) eval(substitute(table(d, x)))
  3. else eval(substitute(table(x, y)), d)
  4. }
  5. tab3(x = mtcars$vs, y = mtcars$cyl)
  6. tab3(mtcars, vs, cyl)
  7. tab3(mtcars$vs, mtcars$cyl)
  8. tab3(mtcars, mtcars$vs, mtcars$cyl)

如果将d放在最后,代码可以简化:

  1. tab4 <- function(x, y, d = parent.frame(), ...) {
  2. eval(substitute(table(x, y)), d)
  3. }
  4. tab4(x = mtcars$vs, y = mtcars$cyl)
  5. tab4(vs, cyl, mtcars)
  6. tab4(mtcars$vs, mtcars$cyl)
  7. tab4(mtcars$vs, mtcars$cyl, mtcars)

也许你可以只使用xtabs。所有这些示例都可以工作并显示名称。

  1. xtabs(~ vs + cyl, mtcars)
  2. xtabs(~ mtcars$vs + mtcars$cyl)
  3. xtabs(~ mtcars$vs + mtcars$cyl, mtcars)
英文:

With this definition the examples below all work as in table. Note that table(mtcars$vs, mtcars$cyl) does not show names so tab3 does not either in those cases. (If table in tab3 were replaced with qtab from collapse then it would show names.)

  1. tab3 &lt;- function(d = parent.frame(), x, y, ...) {
  2. if (missing(y)) eval(substitute(table(d, x)))
  3. else eval(substitute(table(x, y)), d)
  4. }
  5. tab3(x = mtcars$vs, y = mtcars$cyl)
  6. tab3(mtcars, vs, cyl)
  7. tab3(mtcars$vs, mtcars$cyl)
  8. tab3(mtcars, mtcars$vs, mtcars$cyl)

The code could be simplified if d were last:

  1. tab4 &lt;- function(x, y, d = parent.frame(), ...) {
  2. eval(substitute(table(x, y)), d)
  3. }
  4. tab4(x = mtcars$vs, y = mtcars$cyl)
  5. tab4(vs, cyl, mtcars)
  6. tab4(mtcars$vs, mtcars$cyl)
  7. tab4(mtcars$vs, mtcars$cyl, mtcars)

Perhaps you could just use xtabs. All of these work and do show names.

  1. xtabs(~ vs + cyl, mtcars)
  2. xtabs(~ mtcars$vs + mtcars$cyl)
  3. xtabs(~ mtcars$vs + mtcars$cyl, mtcars)

huangapple
  • 本文由 发表于 2023年2月8日 21:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定