On 12/06/2013 10:43 AM, William Dunlap wrote:
I have been researching and it appears that I should be using the sapply
function to apply the evaluate.question function above to each row in
the data frame like this
Read the documentation more closely: sapply(dataFrame, func)
applies func() to each column, not row, of dataFrame.
I misunderstood.  I thought it was apply the func to each row...  My mistake
preferences <- sapply(df, evaluate.questions, function(x,y,z)
evaluate.questions(df['Q1'],df['Q2'],df['Q3']))
Furthermore,
     sapply(X = dataFrame, FUN = func, extraArgument)
calls
     func(dataFrame[, i], extraArgument)
for i in seq_len(ncol(dataFrame).

One problem is that FUN=evaluate.questions takes 3 arguments and
you give it only 2.  Another problem is that the third argument you
pass to sapply is a function (of 3 arguments) and FUN is not expecting
any of its arguments to be functions.
I will need to think about this, I am not sure I understand. I really don't seem to understand how any of the apply functions seem to work.
It may be easier for you to not use sapply here, but to use for-loops and
come up with something that works.  (Write tests that will indicate whether
it works or not in a variety of situations.)  Then transform it to use things
like ifelse() and sapply() to make it more readable and run faster.
I already have tested my functions by using a for loop, and they work. Here is the for loop I use.

for (indx in 1:length(df$ID)) {
df$Preference <- evaluate.questions(df$Q1[indx],df$Q2[indx],df$Q3[indx])
}

I understand that such for loops aren't 'best practice' in R and am trying to learn its approach. Thank you for the suggestions!
Unfortunately this doesn't work and the problem appears that the sapply
function is not feeding the parameters to the evaluate.questions
function as I expect.  Can someone provide some guidance on what I am
doing wrong?
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com



______________________________________________
R-help@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.

Reply via email to