[R] Taking the sum of only some columns of a data frame
Mathew Guilfoyle
mrguilfoyle at gmail.com
Sat Apr 1 01:18:23 CEST 2017
This does the summation you want in one line:
#create example data and column selection
d = as.data.frame(matrix(rnorm(50),ncol=5))
cols = c(1,3)
#sum selected columns and put results in new row
d[nrow(d)+1,cols] = colSums(d[,cols])
However, I would agree with the sentiments that this is a bad idea; far better to have the mean values stored in a new object leaving the original data table untainted.
> On 31 Mar 2017, at 17:20, Bruce Ratner PhD <br at dmstat1.com> wrote:
>
> 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.
More information about the R-help
mailing list