[R] Taking the sum of only some columns of a data frame
Doran, Harold
HDoran at air.org
Fri Mar 31 18:35:52 CEST 2017
Apologies, my code below has an error that recycles the vector x. Hopefully, the concept is clear.
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Doran, Harold
Sent: Friday, March 31, 2017 12:34 PM
To: 'Bruce Ratner PhD' <br at dmstat1.com>; r-help at r-project.org
Subject: Re: [R] Taking the sum of only some columns of a data frame
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
rbind(dat,x)
-----Original Message-----
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
Hi R'ers:
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
______________
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.
______________________________________________
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
mailing list