Probably the following example would explain how you can use sapply based on an index:
x<-matrix(1:9,ncol=3)
sapply(c(1,3),function(i) sum(x[,i]))
Steve
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu on behalf of Hunsicker, Lawrence
Sent: Sun 28/12/2008 12:09
To: s-news@lists.biostat.wustl.edu
Subject: [S] sapply to automate doing a series ofparallel analyses
So here I am back to the trough for help. You guys are really teaching
me how to program S-Plus, and I appreciate the help.
So I now have a function named testScrMiss, which does a mixed model
logistic regression to test which other values in a data set predict
whether a serum creatinine determination was obtained. (You all know
the function from my last messages.)
The function now works fine, with a single integer parameter indexing
the specific column that I want to test. I'd now like to run this
function on a long list of columns - indexed by
c(5:7,11:12,15:16,20,27:29,32:33,38:42,45,50,55:64,72,77:93,99:100,107)
to be specific. So I try:
list1 <-
c(5:7,11:12,15:16,20,27:29,32:33,38:42,45,50,55:64,72,77:93,99:100,107)
sapply(list1, testScrMiss)
Aaarrrrgghh! I get the following error:
Problem in FUN(...X.sub.i....): Length of x (variable 2) is 5 != length
of others (24151)
Use traceback() to see the call stack
So I use traceback to see if I can figure out why S-Plus thinks that
some variable is length 5. This turns out to be the number of columns
in my original data.frame that I reference in the function - the
dependent and independent variable in the fixed formula and the three
nested levels in the random formula.
Now if I run a familiar function like sqrt (remembering that I have a
column of integer indices),
list1 <-
c(5:7,11:12,15:16,20,27:29,32:33,38:42,45,50,55:64,72,77:93,99:100,107)
sapply(list1, sqrt)
I get the expected outcome - a list of the square roots of my original
list.
Now I am suspecting that the latter works because the function sqrt is
polymorphic, with different functions for a single integer and for a
list of integers. But I have been flailing around, trying lots of
different ways to get this thing to work, and I have not succeeded yet.
So I am again asking for help.
Meanwhile I am going back to Tim Hesterbereg's comments on scoping
problems to see I can understand what is going on.
Again, thanks to all for your repeated help.
Larry Hunsicker
|