R:在用户定义的函数中使用get和data.table

时间:2017-08-31 13:25:15

标签: r data.table user-defined-functions

我正在学习如何在R中编写使用常见软件包(如data.table和dplyr)的函数。

我写的这个函数计算特定类别中观察的百分比,在其他一些组中(例如:2015年发布的10-20mpg汽车的份额)并生成一个表格。这里它没有功能:

library(data.table)
library(scales)


#Create test dataframe and cut off points
test<-data.frame(x=c(0:10), y=c(rep(1,5),rep(2,6)), z=c("A","A","A","B","B","B","C","C","C","C","C"))
test <- data.table(test)


#trial non function version (calculating share of row by category z): works
tmp<-test[,.(N=.N), keyby=.(y,z)]
tmp[,total:=sum(N), by=y]
tmp[,percent:=percent(N/total)]
dcast(tmp,y ~ z, value.var="percent")

但是为了让它在一个函数中工作,我必须使用get。一旦评估了get,两个分类变量就必须被称为&#34; get&#34;和&#34; get.1&#34;其余的代码(见下文)。有办法避免这种情况吗?

#Two way table function: data.table

tw_tab<-function(dt,v1,v2){

#set up variables as charaters
v1<-as.character(substitute(v1))
v2<-as.character(substitute(v2))
dt<-as.character(substitute(dt))

#function
tmp<-get(dt)[,.(N=.N), keyby=.(get(v1),get(v2))]
tmp[,total:=sum(N), by=get]
tmp[,percent:=percent(N/total)]
dcast(tmp,get ~ get.1, value.var="percent")

}

#test function
tw_tab(test, y, z)

我尝试使用&#34; get(v1)&#34;和&#34;得到(v2)&#34;在整个代码中,但这不起作用

我已经使用data.table查看了有关用户功能的其他帖子(例如Get a user-defined function work in data.table)但他们似乎没有触及此问题/遇到它。

我对此不熟悉,所以我们将不胜感激任何有关更好地实现这一目标的其他反馈/评论。

2 个答案:

答案 0 :(得分:2)

您无需在get上致电dt(根据我的经验,get最常用于引用使用字符串的列)并且您可以提供字符向量到bykeyby

tw_tab <- function(dt,v1,v2){

    #set up variables as charaters
    v1<-as.character(substitute(v1))
    v2<-as.character(substitute(v2))

    #function
    tmp <- dt[,.(N=.N), keyby = c(v1, v2)]
    tmp[,total:=sum(N), by= c(v1)]
    tmp[,percent:=percent(N/total)]
    dcast(tmp, paste(v1, '~', v2), value.var="percent")
}

#test function
tw_tab(test, y, z)
#    y     A     B     C
# 1: 1 60.0% 40.0%    NA
# 2: 2    NA 16.7% 83.3%

以下是使用xtabsprop.table的解决方案:

tw_tab <- function(x, v1, v2){
    fm <- bquote(~ .(substitute(v1)) + .(substitute(v2)))
    res <- prop.table(xtabs(formula = fm, data = x), 1)
    res <- as.data.frame.matrix(res)
    res[] <- lapply(res, scales::percent)
    return(res)
}

tw_tab(test, y, z)
#     A     B     C
# 1 60% 40.0%  0.0%
# 2  0% 16.7% 83.3%

答案 1 :(得分:1)

我做......

row_pct = function(DT, fm){
  all = all.vars(fm)
  lhs = all.vars(fm[[2]])
  rhs = all.vars(fm[[3]])

  DT[, .N, by=all][, 
    p := percent(N/sum(N)), by=lhs][, 
    dcast(.SD, eval(fm), value.var = "p", fill = percent(0))]
}

示例:

row_pct(test, y ~ z)

   y   A     B     C
1: 1 60%   40%    0%
2: 2  0% 16.7% 83.3%

row_pct(data.table(mtcars), cyl + gear ~ carb)

   cyl gear    1     2     3     4    6   8
1:   4    3 100%    0%    0%    0%   0%  0%
2:   4    4  50%   50%    0%    0%   0%  0%
3:   4    5   0%  100%    0%    0%   0%  0%
4:   6    3 100%    0%    0%    0%   0%  0%
5:   6    4   0%    0%    0%  100%   0%  0%
6:   6    5   0%    0%    0%    0% 100%  0%
7:   8    3   0% 33.3% 25.0% 41.7%   0%  0%
8:   8    5   0%    0%    0%   50%   0% 50%

如果由于某种原因你想分别输入row和col vars:

row_pct2 = function(DT, rowvars, colvar){
      fm  = substitute(`~`(rowvars, colvar))
      row_pct(DT, fm)
}

# Examples:
row_pct2(test, y, z)
row_pct2(data.table(mtcars), cyl + gear, carb)