r - Programmatically sum columns of a data frame? -
suppose have following data frame:
dd <- data.frame(x1 = c(1, 2, 3), x2 = c(3, 4, 5), x3 = (5, 6, 7))
i want create new column, so:
dd$x.sum <- dd$x1 + dd$x2 + dd$x3
but instead of writing out addition (suppose have 100 columns x1, x2, ..., x100
want sum together), want write programmatically. like:
dd$x.sum <- sum(sapply(1:3, function(i) { return(dd[paste0("x", i)]) }))
except doesn't work.
how do this?
you use reduce
+
, although won't have na.rm
argument
dd <- data.frame(matrix(runif(1e6),ncol=1000)) dd$sum <- reduce('+', dd[paste0('x',seq_len(1000))]) # benchmarking xx <- paste0("x", seq_len(1000)) library(microbenchark) microbenchmark(reduce('+', dd[xx]), rowsums(dd[xx])) ## unit: milliseconds ## expr min lq median uq max neval ## reduce("+", dd[xx]) 14.93642 15.19713 15.56077 15.78606 31.92162 100 ## rowsums(dd[xx]) 30.79629 31.44574 31.67192 32.14342 58.07938 100
Comments
Post a Comment