"Error in eval(predvars, data, env) : object not found" when running my code in a function

Hello,
I have the following data frame from a complex survey design. Note that it has survey weights, and bootstrap weights. The variables of interest here is iron (IRO). I want to estimate it’s mean by pregnant and non-pregnant status.

status IRO WGT_FULL BSW1 BSW2 BSW3
pregnant 45 29316.4 0 58050.94 0
pregnant 17 401.9984918 545.8072449 561.8532389 504.8898659
pregnant 32 2203.624 0 4915.119 5293.266
pregnant 33 779.2059197 2304.176342 2633.064775 0
pregnant 17 8958.373 0 0 10007.5
non-pregnant 52 3307.726 0 5982.301 4324.108
non-pregnant 16 304.3146592 567.9649596 0 329.7168172
non-pregnant 67 425.8173 0 0 1285.752
non-pregnant 29 974.3875169 0 1191.837293 0
non-pregnant 19 2502.044 2963.788 2939.72 2785.933
non-pregnant 9 2210.339 2674.871 0 2891.036
non-pregnant 19 642.0245 0 765.6621 649.7655
non-pregnant 12 1322.31 0 1497.407 1383.725
non-pregnant 4 574.426 1455.648 1414.688 0
non-pregnant 73 126.7128 0 189.9741 181.5099
non-pregnant 14 676.4648 0 718.8922 842.7405
non-pregnant 18 4027.879 4874.392 0 5268.307
non-pregnant 97 2196.652 0 3226.484 3420.65
non-pregnant 7 1993.748 0 0 3269.434
non-pregnant 20 485.4677 606.3933 2182.711 0

#Defining the survey design
library("survey")
SurvDesign = svrepdesign(
data = myDat,
weights = ~WGT_FULL,
repweights = "BSW[1-9]+",
combined.weights = T,
type = "BRR")

To estimate the mean by status I do the following and it works perfectly.
mean = as.data.frame(svyby(~IRO, ~status,
SurvDesign,
svymean, na.rm = T, df=33))

Nevertheless, when I define a function that does exacty the same, I get the error message:
Error in eval(predvars, data, env) : object 'IRO' not found

I define and call the function using the following code;
#Defining a function to pass multiple nutrients

myFunc = function(nutr, myDesign, myDF){
mean = as.data.frame(svyby(~nutr, ~status,
myDesign,
svymean, na.rm = T, myDF))
}

myFunc(nutr = IRO, myDesign = SurvDesign, myDF = 33)

Can anyone tell me how to fix this?
Thanks a lot,
AG.

Additional relevant information:
OS: Windows 10 (64-bit)
R version: 3.6.2
R studio version: 3.5

Try

myFunc(IRO, myDesign = SurvDesign, myDF = 33)
(exdf <- data.frame(x=1:3,
                   y=2:4))

lm(y~x,data = exdf)

#will fail with
#Error in eval(predvars, data, env) : object 'x' not found
myfunc <- function(v){
  lm(y~v,data = exdf)
}
myfunc(x)

#convenient to make the formula as a string
myfunc <- function(vc){

  lm(as.formula(paste0("y~",vc)),
     data = exdf)
}
myfunc("x")

That didn't work.
Thank you!

Hi nirgrahamuk,
That's smart. Alternatively, a colleague of mine also suggested me the following and it worked.

IRO =myFunc(nutr=myDat$IRO,
status=myDat$status,
myDesign = SurvDesign,
myDF = 33)

Thank you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.