I have a data frame of questionnaire data in wide format, with each column representing a questionnaire item.
The data looks something like the following:
df <- data.frame(Q1 = c(1, 4, 2, 3, 1, 1, 4, 4, 1, 2),
Q2 = c(NA, 3, 1, 4, NA, NA, 3, 4, 1, 2),
Q3 = c(3, 4, 1, 2, 4, NA, NA, 1, 1, 2),
Q4 = c(NA, 4, 1, 1, 1, 3, NA, 2, 2, NA))
I want to use the rowSums
function to sum up the values in each row that are not "4" and to exclude the NAs and divide the result by the number of non-4 and non-NA columns (using a dplyr pipe). I do not want to replace the 4s in the underlying data frame; I want to leave it as it is.
As I do not know how to divide the result by the number of non-4 and non-NA columns, I have only tried attempting the first part of my question. I have used the following codes to attempt the first part, but it did not work:
library(dplyr)
df <- df %>%
as.data.frame() %>%
mutate(sum = rowSums(.[. != 4, ], na.rm = TRUE))
The desired output would look something like the following:
In the screenshot above, the "mean" column is the sum of non-4 and non-NA values divided by the number of non-4 and non-NA columns.
Thanks!