Here are two possible solutions to your questions.
library(datapasta)
library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
# (1) How to I select specific variables that do not begin with the same
# string? e.g. What if I wanted to calculate the mean based on amb1, amb2,
# and sercli1 and sercli2?
# (2) How do I round off the calculated means to let's say 1 decimal place?
DF <- data.frame(
amb1 = c(2, 3, 2, 4, 6, 7, 3, 5),
amb2 = c(3, 2, 4, 1, 3, 5, 6, 4),
sercli1 = c(4, 6, 3, 5, 5, 4, 4, 6),
sercli2 = c(3, 3, 4, 6, 6, 6, 3, 4))
DF %>%
dplyr::mutate(means = rowMeans(
# this is where you can drop a select
dplyr::select(.,
# with a dplyr::one_of()
dplyr::one_of(
# then add the columns
c('amb1','amb2','sercli1'))),
# remove any missing
na.rm = TRUE),
# round the mean value
means = base::round(means, digits = 2))
#> amb1 amb2 sercli1 sercli2 means
#> 1 2 3 4 3 3.00
#> 2 3 2 6 3 3.67
#> 3 2 4 3 4 3.00
#> 4 4 1 5 6 3.33
#> 5 6 3 5 6 4.67
#> 6 7 5 4 6 5.33
#> 7 3 6 4 3 4.33
#> 8 5 4 6 4 5.00
Created on 2019-05-23 by the reprex package (v0.3.0)
Hope this works!