r data.table函数通过多个变量进行聚合
我一直在尝试推广一个函数,在这个函数中,我使用不同的键进行许多不同的聚合,然后将它们合并在一起。 首先是一个示例data.table:r data.table函数通过多个变量进行聚合,r,data.table,eval,R,Data.table,Eval,我一直在尝试推广一个函数,在这个函数中,我使用不同的键进行许多不同的聚合,然后将它们合并在一起。 首先是一个示例data.table: DT <- data.table( Key1=c("giallo","giallo","giallo","verde","verde","verde","verde","v
DT <- data.table(
Key1=c("giallo","giallo","giallo","verde","verde","verde","verde","verde"),
Key2=c("M","M","L","S","M","M","M","L"),
Filtro=c(1,1,1,0,0,0,1,1),
Var1=c(1,4,5,3,7,5,8,1),
Var2=c(11,24,15,33,17,45,38,21)
)
DT如何将这些键作为1个向量而不是2个向量传递
Tavola <- function(s,chiaves)
{
Tavola1 <- s[,
list(
Somma11=sum(Var1),
Somma12=sum(Var2),
Media11=mean(Var1),
Media12=mean(Var2)),
by=chiaves
]
print(Tavola1)
Tavola2 <- s[Filtro==1,
list(
Somma21=sum(Var1),
Somma22=sum(Var2),
Media21=mean(Var1),
Media22=mean(Var2)),
by=chiaves
]
print(Tavola2)
Tavola3 <- s[Filtro==1 & Var1>3,
list(
Somma32=sum(Var2),
Media32=mean(Var2)),
by=chiaves
]
print(Tavola3)
mymerge = function(x,y) merge(x,y,by=(chiaves),all=TRUE)
TavolaFinale <- Reduce(mymerge,list(Tavola1,Tavola2,Tavola3))
return(TavolaFinale)
}
TavolaStat <- Tavola(s=DT,c("Key1","Key2"))
Tavola感谢您的编辑。你不能用c(chiave1,chiave2)
代替这个列表
和eval
业务吗?使用c()
,当一个元素为NULL
时,它将从向量中删除。@Frank:第一个函数将chiave1和chiave2作为参数,但在函数体中仅使用chiave1。第二个函数尝试使用both@Frank,谢谢,它很管用。我一直使用by中的列表作为data.table
Tavola <- function(s,chiave1,chiave2=NULL)
{
Tavola1 <- s[,
by=list(eval((chiave1)),eval(chiave2)),
list(
Somma11=sum(Var1),
Somma12=sum(Var2),
Media11=mean(Var1),
Media12=mean(Var2)
)
]
print(Tavola1)
Tavola2 <- s[Filtro==1,
by=list(eval(chiave1),eval(chiave2)),
list(
Somma21=sum(Var1),
Somma22=sum(Var2),
Media21=mean(Var1),
Media22=mean(Var2)
)
]
print(Tavola2)
Tavola3 <- s[Filtro==1 & Var1>3,
by=list(eval(chiave1),eval(chiave2)),
list(
Somma32=sum(Var2),
Media32=mean(Var2)
)
]
print(Tavola3)
mymerge = function(x,y) merge(x,y,by=c(chiave1,chiave2),all=TRUE)
TavolaFinale <- Reduce(mymerge,list(Tavola1,Tavola2,Tavola3))
return(TavolaFinale)
}
> TavolaStat <- Tavola(s=DT,chiave1="Key1")
Show Traceback
Rerun with Debug
Error in `[.data.table`(s, , by = list(eval((chiave1)), eval(chiave2)), :
column or expression 2 of 'by' or 'keyby' is type NULL. Do not quote column names. Usage: DT[,sum(colC),by=list(colA,month(colB))] >
> TavolaStat <- Tavola(s=DT,chiave1="Key1",chiave2="Key2")
Show Traceback
Rerun with Debug
Error in `[.data.table`(s, , by = list(eval((chiave1)), eval(chiave2)), :
The items in the 'by' or 'keyby' list are length (1,1). Each must be same length as rows in x or number of rows returned by i (8).
Tavola <- function(s,chiaves)
{
Tavola1 <- s[,
list(
Somma11=sum(Var1),
Somma12=sum(Var2),
Media11=mean(Var1),
Media12=mean(Var2)),
by=chiaves
]
print(Tavola1)
Tavola2 <- s[Filtro==1,
list(
Somma21=sum(Var1),
Somma22=sum(Var2),
Media21=mean(Var1),
Media22=mean(Var2)),
by=chiaves
]
print(Tavola2)
Tavola3 <- s[Filtro==1 & Var1>3,
list(
Somma32=sum(Var2),
Media32=mean(Var2)),
by=chiaves
]
print(Tavola3)
mymerge = function(x,y) merge(x,y,by=(chiaves),all=TRUE)
TavolaFinale <- Reduce(mymerge,list(Tavola1,Tavola2,Tavola3))
return(TavolaFinale)
}
TavolaStat <- Tavola(s=DT,c("Key1","Key2"))