I have a data frame of questionnaire data in wide format, with each column representing one questionnaire item.
Individually, I know how to recode the values within columns and create new columns based on values found in other columns. However, I am experiencing problems trying to do both in a single pipe.
My data looks like the following:
df <- data.frame(Q1 = c(1, 2, 1, 4), Q2 = c(4, 2, 3, 1), Q3 = c(3, 3, 2, 3),
Q4 = c(4, 4, 2, 4), Q5 = c(4, 2, 3, 1), Q6 = c(7, 2, 3, 1))
Using my sample dataset as an example, I intend to subtract 1 from columns Q1, Q2, and Q3 and replace the original values with the new (subtracted) values. Concurrently, I want to create a new column that contains the mean of Q1, Q2, and Q3 while ignoring any NA values or values that are 3.
I have tried the following code, but the Q1, Q2, and Q3 columns are not updated with the subtracted value.
library(dplyr)
df$mean <- df %>%
select(Q1, Q2, Q3) %>%
mutate_all(funs(. - 1)) %>%
apply(1, function(x) {
round(mean(x[!is.na(x) & x != 3]), digits = 2)
})
I have tried using mutate_at followed by mutate in a pipe. However, the end result deletes every other column that is not selected. I still want the other columns to be in the final dataset:
df <- df %>%
select(Q1, Q2, Q3) %>%
mutate_all(funs(. - 1)) %>%
mutate(mean = apply(., 1, function(x)
round(mean(x[!is.na(x) & x != 3]), digits = 2)))
Thanks and much appreciated!