dplyr - R: colSums & Mutate (Wrong Result Size) -


i trying mutate colsums @ bottom of dataframe, first column of table character vector containing labels.

for example,

df=data.frame(   label = c("a","b","c","d","e","f","g","h","i","j"),   x1=c(1,0,0,na,0,1,1,na,0,1),   x2=c(1,1,na,1,1,0,na,na,0,1),   x3=c(0,1,0,1,1,0,na,na,0,1),   x4=c(1,0,na,1,0,0,na,0,0,1),   x5=c(1,1,na,1,1,1,na,1,0,1)) 

without label col,

df %>% mutate(total = colsums(df[, 1:5], na.rm = true)) 

should work fine... tried

df %>% mutate(total = colsums(df[, 2:6], na.rm = true)) 

which gave me error message

error in mutate_impl(.data, dots) : wrong result size (5), expected 10 or 1

how can ignore first column , still mutate colsums bottom of data frame?

thank you.

mutate adds new column data.frame. indicate you're trying add new row bottom. error message: in trying create new column, mutate expects vector of length 10 (or single value can fill entire column with).

if want add totals row data.frame, try janitor::add_totals_row:

library(janitor)  df %>%   add_totals_row()  #>    label x1 x2 x3 x4 x5 #> 1       1  1  0  1  1 #> 2      b  0  1  1  0  1 #> 3      c  0 na  0 na na #> 4      d na  1  1  1  1 #> 5      e  0  1  1  0  1 #> 6      f  1  0  0  0  1 #> 7      g  1 na na na na #> 8      h na na na  0  1 #> 9       0  0  0  0  0 #> 10     j  1  1  1  1  1 #> 11 total  4  5  4  3  7 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -