Calculating Sum Column and ignoring Na [duplicate]

笑着哭i 提交于 2020-01-30 05:19:12

问题


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!!


回答1:


Add in na.rm=TRUE

i.e

rowSums(na.rm=TRUE)



回答2:


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



来源:https://stackoverflow.com/questions/48689693/calculating-sum-column-and-ignoring-na

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!