Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Sum Column and ignoring Na [duplicate]

Tags:

r

na

rowsum

I am trying to create a Total sum column that adds up the values of the previous columns. However I am having difficulty if there is an NA. If there is an NA in the row, my script will not calculate the sum.

How do I edit the following script to essentially count the NA's as 0, or just ignore them completely but still calculate the sum. I don't want to actually change the NA to 0.

CTDB %>% mutate(SCARED_TOTAL_CHILD = CTDB %>% rowwise() %>% 
SELECT(SCARED_BREATHE_CHILD:SCARED_SHY_CHILD) %>% rowSums()) -> CTDB

Thank you!!

like image 335
KKhosra Avatar asked Dec 24 '22 10:12

KKhosra


2 Answers

Add in na.rm=TRUE

i.e

rowSums(na.rm=TRUE)
like image 144
MLPNPC Avatar answered Jan 15 '23 10:01

MLPNPC


ummm, I think the help for the function outlines how to omit NAs from the calc.... try rowSums(.,na.rm=T)

like image 43
mando_R Avatar answered Jan 15 '23 09:01

mando_R