My dataset looks like this:
values<-c(9,8,NA,8)
acceptance<-c(8,8,NA,6)
diffusion<-c(9,8,7,NA)
attitudes<-c(7,7,6,NA)
df<-data.frame(values,acceptance,diffusion,attitudes)
values acceptance diffusion attitudes
9 8 6 8
8 8 8 7
NA NA 7 6
8 6 NA NA
I can use the code mean(df$values, na.rm = T) to get the mean for each column, but I'd like to create a new variable for the total mean of specific columns (ex: values & acceptance). I could just use the same code for each column I want to include and then figure out the mean that way:
mean(df$values, na.rm = T) = 8.333
mean(df$acceptance, na.rm = T) = 7.333
(8.333 + 7.333)/2 = 7.833
df$values_acceptance<-7.833
But this would be really inefficient because I have multiple variables I need to include. I'm sure there is a much easier way to do this, but I'm still getting used to R.
Thanks in advance!