0

I'm new to R and I use the package MICE for multiple imputation. For basic analysis I want to calculate mean for different columns using subsets. I have > 70 columns named "Var_1", "Var_2" and so on. The following statement works so far:

meanVal <- with(imp, exp = mean(subset(Var_1, Var_2 == 1)))
saveValue <- mean(unlist(meanVal$analyses))

At this point I want to use a function, because I have different conditions and a list of vars for which I want to calculate mean values.

doMean(imp, varlist, filter) {
    for (i in 1:length(varlist) {
        meanVal <- with(imp, exp = mean(subset(varlist[i], Var_2 == filter)))
        saveValue[i] <- mean(unlist(meanVal$analyses))
    }
}
#print(varlist[1]) is "Var_1"
#print(varlist[2]) is "Var_4"
#...

The expression varlist[i] is incorrect and I have no idea how to typecast it correctly. The warning message is argument is not numeric or logical: returning NA. I have tried noquote() and different as.function_name()-functions.

Edit: A small example

library(mice)
data("nhanes2")

head(nhanes2)

impZero <- mice(nhanes2, maxit = 0)
imp <- mice(nhanes2, maxit = 5, method = impZero$method, predictorMatrix = impZero$predictorMatrix, printFlag = FALSE)

rname <- c('chl', 'bmi')

#Working
MeanRes <- with(imp, exp = mean(subset(chl, bmi > 25)))
res <- mean(unlist(MeanRes$analyses)) #200.688888888
enter code here
#Not Working
MeanRes <- with(imp, exp = mean(subset(rname[1], bmi > 25)))
res <- mean(unlist(MeanRes$analyses)) #NA

Thank you very much in advance for your help!

rNewbie
  • 1
  • 1
  • 1
    Welcome to SO, rNewbie! You will be most likely to get your question answered if you follow the [helpful advice](https://stackoverflow.com/help/how-to-ask) about asking questions and create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – DaveArmstrong Nov 01 '21 at 17:20
  • The problem is that `rname` contains text/characters with your variable names but not the variables themselves. Replacing `rname[1]` with `get(rname[1])` should work. (I am not sure, however, if this is considered good practice.) – benimwolfspelz Nov 17 '21 at 09:12
  • @benimwolfspelz Thank you very much, my problem is solved. `rname` contains information from an external csv-file and they could change from time to time, so for me, it was the easiest way to get what I need without hundreds of if-clauses. :) But for now everything works fine. – rNewbie Nov 19 '21 at 16:54

0 Answers0