R聚合函数 - 可变长度不同

时间:2016-01-27 06:25:00

标签: r

我想在自定义函数中调用aggregate。但是,它给了我一个错误

Error in model.frame.default(formula = cbind(A, B) ~ level + depvar, data = dataset,  : 
  variable lengths differ (found for 'depvar')

我的代码如下:

Dep <- c(1,0,1,1,1,1,0,0,0,1)  
A <- c(20, 3, 1,32,43,43,54,32,43,53)  
B <- c(543,543,656,243,532,643,423,543,532,984)  
temp <- data.frame(Dep,A,B)

my.function<-function(dataset,depvar,indvar,rank=5){

    dataset$level<-cut(dataset[,indvar], breaks= c(quantile(dataset[,indvar], probs = seq(0, 1, by = 1/rank))),include.lowest= TRUE, labels= c(1:rank),ordered = FALSE)
    table(dataset$level)
    depvar<-noquote(depvar)
    aggregate(cbind(A,B) ~ level+ depvar, data = dataset, FUN = length, na.action = na.omit)
    }
my.function(dataset=temp, depvar="Dep",indvar="A")

1 个答案:

答案 0 :(得分:1)

您需要在paste0函数中as.formulaaggregate的组合,例如:

as.formula(paste0("cbind(A,B) ~ level + ", depvar))
# cbind(A, B) ~ level + Dep

所以你的函数变成了(我删除了table(dataset$level),这在这个函数中没用):

my.function <- function(dataset,depvar,indvar,rank=5){
    dataset$level<-cut(dataset[,indvar], breaks= c(quantile(dataset[,indvar], probs = seq(0, 1, by = 1/rank))),include.lowest= TRUE, labels= c(1:rank),ordered = FALSE)
    aggregate(as.formula(paste0("cbind(A,B) ~ level + ", depvar)), data = dataset, FUN = length, na.action = na.omit)
}
my.function(dataset=temp, depvar="Dep",indvar="A")

#   level Dep A B
# 1     1   0 1 1
# 2     2   0 1 1
# 3     3   0 1 1
# 4     5   0 1 1
# 5     1   1 1 1
# 6     2   1 2 2
# 7     3   1 2 2
# 8     5   1 1 1