Reputation: 1
I am trying to create a variable using the mutate function which is the mean score of multiple other variables while excluding observations that have more than one missing value. The code I am using will just factor out the missing value from it's calculation and continue to calculate the mean.
h18lb_r <- h18lb_r %>% mutate(social_network_contact_mean = rowMeans(h18lb_r[c(QLB008A, QLB008B, QLB008C, QLB008D, QLB012A, QLB012B, QLB012C,QLB012D, QLB016A, QLB016B, QLB016C, QLB016D), na.rm=TRUE]))
Error in `mutate()`:
ℹ In argument: `social_network_contact_mean = rowMeans(...)`.
Caused by error in `[.data.frame`:
! unused argument (na.rm = TRUE)
I would like a new column with the mean score for the combined variables while excluding observations that have more than one missing value.
Upvotes: 0
Views: 24
Reputation: 1
I think you may be subsetting the rowMeans function wrong. There are multiple ways to subset but this one worked for me!
data <- data.frame(x=c(4,3,4,4,99),
y=c(4,NA,3,2,4),
z = c(88,NA,4,4,5),
w = c(4,5,2,3,4))
h18lb_r <- data %>% mutate(avg_mean = rowMeans(subset(data, select = c(`x`,`y`,`z`,`w`)), na.rm = T))
Upvotes: 0