[R] Taking the sum of only some columns of a data frame

William Michels wjm1 at caa.columbia.edu
Fri Mar 31 22:05:09 CEST 2017


Again, you should always copy the R-help list on replies to your OP.

The short answer is you **shouldn't** replace NAs with blanks in your
matrix or dataframe.  NA is the proper designation for those cell
positions. Replacing NA with a "blank" in a dataframe will convert
that column to a "character" mode, precluding further numeric
manipulation of those columns.

Consider your workflow:  are you tying to export a table? If so, take
a look at installing pander (see 'missing' argument on webpage below):

https://cran.r-project.org/web/packages/pander/README.html

Finally, please review the Introductory PDF, available here:

https://cran.r-project.org/doc/manuals/R-intro.pdf

HTH, Bill.

William Michels, Ph.D.



On Fri, Mar 31, 2017 at 11:21 AM, BR_email <br at dmstat1.com> wrote:
> William:
> How can I replace the "NAs" with blanks?
> Bruce
>
> Bruce Ratner, Ph.D.
> The Significant Statistician™
>
>
> William Michels wrote:
>>
>> I'm sure there are more efficient ways, but this works:
>>
>>> test1 <- matrix(runif(50), nrow=10, ncol=5)
>>> ## test1 <- as.data.frame(test1)
>>> test1 <- rbind(test1, NA)
>>> test1[11, c(1,3)] <- colSums(test1[1:10,c(1,3)])
>>> test1
>>
>>
>> HTH,
>>
>> Bill.
>>
>> William Michels, Ph.D.
>>
>>
>>
>> On Fri, Mar 31, 2017 at 9:20 AM, 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