This page contains common questions & some answers! More coming soon!
Change the parts in CAPS to match your data!
DATA <- DATA %>%
dplyr::rowwise() %>%
dplyr::mutate(
NEW_COLUMN_NAME = mean(c(VARIABLE1, VARIABLE2, VARIABLE3)))
Any rows with missing data will have NA in the new column
DATA <- DATA %>%
dplyr::rowwise() %>%
dplyr::mutate(
NEW_COLUMN_NAME = mean(c(VARIABLE1, VARIABLE2, VARIABLE3), na.rm = TRUE))
By including the na.rm argument, we can calculate the mean of our variables, even where there is missing data in any of them
VARIABLE_MEAN <- DATA %>%
dplyr::select(VARIABLE1, VARIABLE2, VARIABLE3) %>%
sjstats::mean_n(., 2) %>%
tibble::as_tibble_col(., column_name = "VARIABLE_MEAN")
DATA <- cbind(DATA, VARIABLE_MEAN)
The first part creates a mean of the variables we have selected, only where there are at least 2 responses (you can use any number here in the mean_n() function), and produces a tibble called varible_mean which contains those values. The second part binds that new column to our existing data