s-news
[Top] [All Lists]

Writing a function taking a data.frame column name as a parameter

To: <s-news@lists.biostat.wustl.edu>
Subject: Writing a function taking a data.frame column name as a parameter
From: "Hunsicker, Lawrence" <lawrence-hunsicker@uiowa.edu>
Date: Fri, 26 Dec 2008 14:57:45 -0600
Thread-index: AclnnJnYPZuBT/I7RXe3wPLTXNh0dQ==
Thread-topic: [S] Writing a function taking a data.frame column name as a parameter

Hi again, folks, and happy Boxing Day to all:

I have written a script that works:

temp <- working7[!is.na(working7$Sexe),]

any.glmm<-glmmPQL(fixed = anyCVD ~ Sexe

        , random = ~1|Region/Pays/Center, family = binomial(link=logit), data = temp)

list("Sexe", anova(any.glmm), any.glmm$coefficients$fixed)

I now want to automate this script to run with a number of columns in a data frame with over 100 columns.  It would be nice to set this up as a function that I could apply with sapply.   I have tried to define the function as follows:

testfx <- function(x) {

temp <- working7[!is.na(working7$x),]

any.glmm<-glmmPQL(fixed = anyCVD ~ x

                , random = ~1|Region/Pays/Center, family = binomial(link=logit), data = "">

list(x, anova(any.glmm), any.glmm$coefficients$fixed)

This compiles.  But when I try to run the functio entering a column name in the place of x 9e.g., testfx(Sexe), it always abends with the message:

Ethnie.glmm <- testfx("Ethnie")

Problem in testfx("Ethnie"): Length of anyCVD (variable 1) is 0 != length of others (10)

Same if I enter the column name  unquoted, or use an index. 

What am I doing wrong?  How does one enter a column name (or column number) as a parameter in a self-defined function?

As always, many thanks to anyone that can help me here.

Larry Hunsicker

<Prev in Thread] Current Thread [Next in Thread>