英文:
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 |> 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 |> 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 |> table(x, y)
).
Native pipe compatible function:
tab1 <- function(d, x, y){
eval(substitute(table(d$x, d$y)))
}
mtcars |> 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 |> 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
答案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 <- 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 <- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论