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

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

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:

tab1 <- function(d, x, y){
  eval(substitute(table(d$x, d$y)))
}

mtcars |&gt; tab1(cyl, vs)
# or
tab1(mtcars, cyl, vs)

R base:

tab2 <- function (x, y) {
  result <- table(x, y)
  result
}

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:

tab(mtcars, cyl, vs) # it's tab1 function 
mtcars |&gt; tab(cyl, vs) # it's tab1 function  
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:

tab1 &lt;- function(d, x, y){
  eval(substitute(table(d$x, d$y)))
}

mtcars |&gt; tab1(cyl, vs)
# or
tab1(mtcars, cyl, vs)

R base:

tab2 &lt;- function (x, y) {
  result &lt;- table(x, y)
  result
}

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:

tab(mtcars, cyl, vs) # it&#39;s tab1 function 
mtcars |&gt; tab(cyl, vs) # it&#39;s tab1 function  
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,它将显示名称。)

tab3 <- function(d = parent.frame(), x, y, ...) {
  if (missing(y)) eval(substitute(table(d, x)))
  else eval(substitute(table(x, y)), d)
}

tab3(x = mtcars$vs, y = mtcars$cyl)
tab3(mtcars, vs, cyl)
tab3(mtcars$vs, mtcars$cyl)
tab3(mtcars, mtcars$vs, mtcars$cyl)

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

tab4 <- function(x, y, d = parent.frame(), ...) {
  eval(substitute(table(x, y)), d)
}

tab4(x = mtcars$vs, y = mtcars$cyl)
tab4(vs, cyl, mtcars)
tab4(mtcars$vs, mtcars$cyl)
tab4(mtcars$vs, mtcars$cyl, mtcars)

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

xtabs(~ vs + cyl, mtcars)
xtabs(~ mtcars$vs + mtcars$cyl)
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.)

tab3 &lt;- function(d = parent.frame(), x, y, ...) {
  if (missing(y)) eval(substitute(table(d, x)))
  else eval(substitute(table(x, y)), d)
}
    
tab3(x = mtcars$vs, y = mtcars$cyl)
tab3(mtcars, vs, cyl)
tab3(mtcars$vs, mtcars$cyl)
tab3(mtcars, mtcars$vs, mtcars$cyl)

The code could be simplified if d were last:

tab4 &lt;- function(x, y, d = parent.frame(), ...) {
  eval(substitute(table(x, y)), d)
}

tab4(x = mtcars$vs, y = mtcars$cyl)
tab4(vs, cyl, mtcars)
tab4(mtcars$vs, mtcars$cyl)
tab4(mtcars$vs, mtcars$cyl, mtcars)

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

xtabs(~ vs + cyl, mtcars)
xtabs(~ mtcars$vs + mtcars$cyl)
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:

确定