R package sqldf sum over multiple columns -
R package sqldf sum over multiple columns -
suppose want summary using sqldf("select id,group, sum(v1),sum(v2),.....sum(90) info grouping id,group"),
since have more 90 variables, there way efficiently typing 90 times ?
thanks!
using built in anscombe
info set add together id
, grp
columns sake of example. create selection string, sel
, , insert sql statement shown:
library(sqldf) anscombe$id <- c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4) anscombe$grp <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2) nms <- setdiff(names(anscombe), c("id", "grp")) # names except id , grp sel <- tostring(sprintf("sum(%s) '%s'", nms, nms)) # "sum(x1) 'x1', ..." fn$sqldf("select id, grp, $sel anscombe grouping id, grp")
giving:
id grp x1 x2 x3 x4 y1 y2 y3 y4 1 1 1 18 18 18 16 14.99 17.28 14.23 12.34 2 2 1 33 33 33 24 24.72 26.77 27.66 25.02 3 3 2 20 20 20 16 17.20 14.23 14.92 12.29 4 4 2 28 28 28 43 25.60 24.23 25.69 32.86
also note easy base of operations r this:
aggregate(. ~ id + grp, anscombe, sum)
r sum typing sqldf
Comments
Post a Comment