The sum() seems work fine for integer, but somehow doesn't work for this dataset in RStudio on my machine.
Here is the data. After ran codes below, The total is 111.6443 for all rows and avg is 37.21476 for all rows. Do you see the same? What library sum () stays? What was wrong?
library(tidyverse)
library(dplyr)
library(tidyr)
test <- data.frame(
stringsAsFactors = FALSE,
PROVNUM = c("15009", "15009", "15010", "15010", "15012"),
SCORE18 = c(NA, 0.735293, 33.027526, 3.382664, 0),
SCORE19 = c(7.344632, 3.030305, NA, 1.106195, 3.252032),
SCORE20 = c(10.778443, NA, 43.902439, 0, 5.084747)
)
df1 <- test%>% mutate(total=sum(c(SCORE18,SCORE19,SCORE20),na.rm=TRUE))
df2 <- df1 %>% mutate(avg=total/3)