[R] Taking the sum of only some columns of a data frame
HDoran at air.org
Fri Mar 31 18:33:57 CEST 2017
I do not believe this can be done in one step
dat <- data.frame(matrix(rnorm(50), 5))
pos <- c(1,3)
res <- apply(dat[, pos], 2, sum)
x <- numeric(5)
x[pos] <- res
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Bruce Ratner PhD
Sent: Friday, March 31, 2017 12:20 PM
To: r-help at r-project.org
Subject: [R] Taking the sum of only some columns of a data frame
Given a data.frame of five columns and ten rows.
I would like to take the sum of, say, the first and third columns only.
For the remaining columns, I do not want any calculations, thus rending their "values" on the "total" row blank. The sum/total row is to be combined to the original data.frame, yielding a data.frame with five columns and eleven rows.
Thanks, in advance.
Bruce Ratner PhD
The Significant Statistician™
[[alternative HTML version deleted]]
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help