How to sum dbl list by columns?

Hello. I was trying to sum the numbers by column in a list like this:

>affect_word <- affect_word %>% replace(.=="null", 0)
>head(affect_word)

 positive negetive anxiouse anger   sad
     <dbl>    <dbl>    <dbl> <dbl> <dbl>
1     6.17        0        0     0     0
2    16           0        0     0     0
3    15.4         0        0     0     0
4    12.5         0        0     0     0
5    15.2         0        0     0     0
6    15           0        0     0     0

Then using colSums to su the colmns

>sum = colSums(affect_word)
>sum
positive negetive anxiouse    anger      sad 
      NA       NA       NA       NA       NA 

How to fix this?

It's probably because one or more of your rows contain NA values.

Can you try colSums(affect_word, na.rm = TRUE)?

base::replace is for replacing values in a vector, but you are passing a dataframe.
its the wrong tool for the job...

affect_word <- affect_word %>% mutate_all(list(~ifelse(is.na(.),0,.)))

or

affect_word[is.na(affect_word )] <- 0

sidenote: package sjmisc includes a replace_na that i find more useful than the tidyverse version

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.