Something like this maybe?:
data.frame(
Map(function(x,y) if(all(is.numeric(x),is.numeric(y))) x * y else x, df1, df2)
)
# a b c d
#1 alpha 7 x 40
#2 beta 16 y 55
#3 gamma 27 z 72
Some benchmarking:
smp <- sample(1:4,50000,replace=TRUE)
df1big <- df1[,smp]
df2big <- df2[,smp]
lmfun <- function() {
out <- data.frame(
Map(function(x,y) if(all(is.numeric(x),is.numeric(y))) x * y else x,
df1big, df2big)
)
}
johnfun <- function() {
sel <- sapply(df1big, is.numeric)
df1big[,sel] <- df1big[,sel] * df2big[,sel]
}
system.time(lmfun())
# user system elapsed
# 6.06 0.00 6.07
system.time(johnfun())
# user system elapsed
# 24.91 0.00 24.99