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.
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.
[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.
Using the rowwise
command within a pipe gets us close
# 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.
# 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 =)