[R] Adding labels to ColSums

Muhuri, Pradip (SAMHSA/CBHSQ) Pradip.Muhuri at samhsa.hhs.gov
Tue Oct 28 16:44:26 CET 2014


Hello,

I was trying to add labels to the colSums  of the  "integers" variable corresponding to a "factor".  Below are the  warning message and the reproducible code.  How would I tweak the code to replace the "NA" with the "Total" in the output?  Your advice toward resolving the issue would be greatly appreciated.

Thanks,

Pradip Muhuri



###################  warning message - from the console  ############################
rb.data <- rbind(s.data2, c("Total", colSums(s.data2[,2, drop=FALSE]))) # row bind with the column total

Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "Total") :
  invalid factor level, NA generated
> rb.data
Source: local data frame [7 x 2]

  years.before.initiated.cat anl.count
1                      [0,1]        89
2                      (1,2]        73
3                      (2,3]        72
4                      (3,4]        82
5                      (4,5]        82
6                      (5,6]        86
7                         NA       484
#########################  reproducible code #################################################
library(dplyr)

i.data2 <- data.frame(sample(1:6, size=484, replace=T)) # simulate data to create a data frame
colnames(i.data2) <- "years.before.initiated" # add a column name
                      
m.data2 <- mutate(i.data2,  years.before.initiated.cat = 
                    cut(years.before.initiated, breaks=c(0,1,2,3,4,5,6),include.lowest=TRUE))
                        # create a new variable

g.data2 <- group_by(m.data2, years.before.initiated.cat) # group by years.before.initiated.cat
s.data2 <- summarise(g.data2, anl.count =n() ) # summarize to get the count

rb.data <- rbind(s.data2, c("Total", colSums(s.data2[,2, drop=FALSE]))) # row bind with the column total
rb.data
###########################################################################################

Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260
ommented, minimal, self-contained, reproducible code.



More information about the R-help mailing list