Mutating Scale Items with NA

Creating item totals with a data set containing NAs is surprisingly difficult. Here is the data.

library(tidyverse)

cd <- data.frame(
  "q1" = c(1,2,NA),
  "q2" = c(2,2,2),
  'q3' = c(NA, NA,2),
  'id' = c('201', '202', '203')
)

cd
  q1 q2 q3  id
1  1  2 NA 201
2  2  2 NA 202
3 NA  2  2 203

Mutating directly over columns with NA does not work.

cd %>%
  mutate(cohesion = 
           q1 + q2 + q3)
  q1 q2 q3  id cohesion
1  1  2 NA 201       NA
2  2  2 NA 202       NA
3 NA  2  2 203       NA

Filtering removes the data we are interested in.

cd %>%
  filter(!is.na(q1) == T && !is.na(q2) == T && !is.na(q3) == T)
[1] q1 q2 q3 id
<0 rows> (or 0-length row.names)

We cannot use rowMeans in combination with mutate because the two are not compatible. The code below is not evaluated, but if you run it it does not work.

cd %>%
  mutate(cohesion =
           rowMeans(q1, q2, q3, na.rm = T))

Using the rowwise command within a pipe gets us close

cd %>%
  rowwise() %>%
  mutate(mean = mean(q1, q2, q3, na.rm = T))
# A tibble: 3 × 5
# Rowwise: 
     q1    q2    q3 id     mean
  <dbl> <dbl> <dbl> <chr> <dbl>
1     1     2    NA 201       1
2     2     2    NA 202       2
3    NA     2     2 203     NaN

but the mean value is not calculated correctly. We need to include c() to vectorize the items.

cd %>%
  rowwise() %>%
  mutate(mean = mean(c(q1, q2, q3), na.rm = T))
# A tibble: 3 × 5
# Rowwise: 
     q1    q2    q3 id     mean
  <dbl> <dbl> <dbl> <chr> <dbl>
1     1     2    NA 201     1.5
2     2     2    NA 202     2  
3    NA     2     2 203     2  

Finally the right answer. Use rowwise in combination with a vectorized mutate.

Bo\(^2\)m =)