Sabatier, Jennifer F. (CDC/OID/NCHHSTP)
2010-May-20 15:24 UTC
[R] Svy function doesn't work nested in user-defined function
Hi R-help,
I posted about this late yesterday but got no response. I may have put
TMI in the original request. Not to mention I couldn't cut and paste
yesterday because I was working R off a non-network computer while
asking for help on a network computer.
Essentially, I have this user-defined function:
test <- function(X){
chisq <- svychisq(~X + SEX, design=audit,
statisic="adjWald", round=4)
}
test(con)
"con" is a data variable in my design object audit.
When I just run:
chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald",
round=4)
It works just fine.
It's only when it's nested in the function test() that it falls apart.
I get this error:
Error in `[.data.frame`(design$variables, , as.character(rows)) :
undefined columns selected
How can I solve this problem?
Thanks,
Jen (obviously a new R-user, as of late 2009)
David Freedman
2010-May-20 16:28 UTC
[R] Svy function doesn't work nested in user-defined function
I'm not sure that this is the problem, but are you certain that the variable 'con' is in audit ? You check outside the function just really tells you that X and SEX are in audit. hth, david freedman (good to see another CDCer using R) -- View this message in context: http://r.789695.n4.nabble.com/Svy-function-doesn-t-work-nested-in-user-defined-function-tp2224732p2224843.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-May-20 20:32 UTC
[R] offlist Re: Svy function doesn't work nested in user-defined function
On May 20, 2010, at 11:24 AM, Sabatier, Jennifer F. (CDC/OID/NCHHSTP) wrote:> > Hi R-help, > > I posted about this late yesterday but got no response. I may have put > TMI in the original request. Not to mention I couldn't cut and paste > yesterday because I was working R off a non-network computer while > asking for help on a network computer. > > > Essentially, I have this user-defined function: > > test <- function(X){ > > > chisq <- svychisq(~X + SEX, design=audit,Did you really have a variable name "X" inside the object that you are giving a temporary name of "X" to. Seems confusing if that were your choice.> statisic="adjWald", round=4) > > } > > test(con) >I do not know the answer, but I'm not sure that anyone including Lumley could answer it with the proffered information: Perhaps you should include the code used to create "audit" as well as the results of str() on the data argument to design when audit was created. I am guessing that "SEX" is not available inside the function and that you should have written: test <- function(Z){ chisq <- svychisq(~X + SEX, design=Z, statistic="adjWald", round=4) } -- David.> > > "con" is a data variable in my design object audit. > > When I just run: > > chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald", round=4) > > It works just fine. > > It's only when it's nested in the function test() that it falls apart. > > I get this error: > > Error in `[.data.frame`(design$variables, , as.character(rows)) : > undefined columns selected > > > How can I solve this problem? > > Thanks, > > Jen (obviously a new R-user, as of late 2009) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT