Answering to Farrel answer:
On RSeek for rowProd I found two packages - matrixStats and fUtilities. You could look on them.
Second solution is bit tricky. You can create you expression and evaluate them.
X <- structure(list(
varA = c(0.98, 0.75, -0.56, -1.43, 0.65, -1.15, -1.52, 0.1, 0.06, 0.76),
varB = c(-0.12, -0.6, 0.62, 0.9, -0.44, 0.37, 0.62, 0.76, -1.61, -0.26),
varC = c(-0.5, -0.37, -0.43, -0.7, 0.83, -0.24, -0.57, 0.05, -1.31, 0.7),
varD = c(-0.06, -0.11, 1.03, -1.76, -0.42, -1.21, -0.62, -1, -1.16, 2.13),
varE = c(-1.96, 0.69, -1.85, -1.74, -1.47, 1.24, 0.29, -1.18, 0.89, 0.42),
varF = c(0.29, -0.22, -1.29, 1.19, 0.38, -0.23, -0.5, -1.07, -1.83, 0.58),
varG = c(0.59, -0.41, -1.37, 0.89, -0.75, 0.95, 0.95, -0.9, 0.71, -1.3)
),
.Names = c("varA", "varB", "varC", "varD", "varE", "varF", "varG"),
row.names = c(NA, -10L), class = "data.frame"
)
metrics <- c("varB","varC","varF")
eval(
parse( text = paste(metrics,collapse=" * ") ),
envir = X
)
Some explanations:
- paste create a string looks like varB * varC * varF (collapse is for concatenating elements of vector)
- parse is to convert text to expression
- eval with envir=X is to execute expression within X
For your original question you could use collapse="+".
edit: if your variables aren't in a data.frame then eval without envir is enough.
edit2: examples of using rowProds from mentioned packages:
matrixStats::rowProds(as.matrix(X[,metrics])) # convert to a matrix is needed
fUtilities::rowProds(X[,metrics]) # without conversion
I digg in source this functions and:
- fUtilities use apply, so this is the same as apply(X,1,prod) (this is not efficient soulution)
- matrixStats is smart and do something like exp(rowSums(log(X))), so should be faster.
Speed tests:
Xm <- matrix(rnorm(50000*8),ncol=8)
Xd <- as.data.frame(Xm)
require(fUtilities)
require(matrixStats)
system.time( matrixStats::rowProds(as.matrix(Xd)) )
# user system elapsed
# 0.08 0.02 0.09
system.time( matrixStats::rowProds(Xm) )
# user system elapsed
# 0.08 0.00 0.08
system.time( fUtilities::rowProds(Xd) )
# user system elapsed
# 0.52 0.00 0.52
Even with conversion to a matrix matrixStats version is faster.