Reputation:
I am totally new to R and Stack Overflow so sorry if I ask this question in a weird way.
I have a dataset which was obtained through surveys. In this survey there is a subset of variables that are grouped together and I would like to get the mean of a subset of these variables.
So for example: I want the averages of columns 18-22 of respondent/row 138 with na.rm=TRUE
Is that possible?
Upvotes: 1
Views: 1745
Reputation: 2323
Can easily be done with dplyr
. In the example df
is the dataset name and columavg
will be the column to store the new value in.
library(dplyr)
df = df %>%
mutate(columavg = case_when(row_number() == 138 ~ rowMeans(.[18:22], na.rm=TRUE),
TRUE ~ 0))
or if you want just that single value
rowMeans(df[138,18:22], na.rm=TRUE)[[1]]
Upvotes: 1