For example:
dt <- data.table()
x=1:5
> dt[,list(2,3,x)]
V1 V2 x
1: 2 3 1
2: 2 3 2
3: 2 3 3
4: 2 3 4
5: 2 3 5
The resulting data table has a column x
For some reason, I would like to create a function to simplify the construction of data.table.
tt <- function(a, b, ...){
list(a=sum(a), b=sum(b), ...)
}
> dt[,tt(1:2,1:3,x)]
a b V3
1: 3 6 1
2: 3 6 2
3: 3 6 3
4: 3 6 4
5: 3 6 5
Therefore, whenever I call list, I use instead tt, so it automatically inserts predefined columns for me. However, now it does not recognize the shortcut name for x.
How to improve ttfor an automatic column name like listin data.table if it is not too complicated?
purpose
dt[,tt(1:2,1:3,x)]
Returns
a b x
1: 3 6 1
2: 3 6 2
3: 3 6 3
4: 3 6 4
5: 3 6 5
Decision
tt <- function(a, b, ...){
dots <- list(...)
inferred <- sapply(substitute(list(...)), function(x) deparse(x)[1])[-1]
if(is.null(names(inferred))){
names(dots) <- inferred
} else {
names(dots)[names(inferred) == ""] <- inferred[names(inferred) == ""]
}
c(a=sum(a), b=sum(b), dots)
}
dt <- data.table(c=1:5)
x=1:5
> dt[,tt(1:2,1:3,x,c+1)]
a b x c + 1
1: 3 6 1 2
2: 3 6 2 3
3: 3 6 3 4
4: 3 6 4 5
5: 3 6 5 6
> dt[,tt(1:2,1:3,x, z=c+1)]
a b x z
1: 3 6 1 2
2: 3 6 2 3
3: 3 6 3 4
4: 3 6 4 5
5: 3 6 5 6
Update
I recently discovered that there was some error on page 46 of S Programming from Venables and Ripley. I made some changes and posted them here. Hope this would be helpful for some people.
name.args <- function(...){
dots <- as.list(substitute(list(...)))[-1]
nm <- names(dots)
if (!is.null(nm) && all(nm != ""))
return(nm)
if (length(dots) == 0)
return(character(0))
fixup <-
if (is.null(nm))
seq(along=dots)
else
nm == ""
dep <- sapply(dots[fixup], function(x) deparse(x)[1])
if (is.null(nm))
dep
else {
nm[fixup] <- dep
nm
}
}