[18877] in s-news-athena

home help back first fref pref prev next nref lref last post

[S] Summary: Speeding up function

daemon@ATHENA.MIT.EDU (=?iso-8859-1?Q?"G=E9rald_Jean"?=)
Thu Aug 5 16:33:40 1999

From: =?iso-8859-1?Q?"G=E9rald_Jean"?= <Gerald.Jean@spgdag.ca>
To: s-news@wubios.wustl.edu
Message-Id: <852567C4.006EE8CB.00@mail.spgdag.com>
Date: Thu, 5 Aug 1999 16:27:35 -0400
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


Hi there,

thanks to all who responded, Charles Berry, S.D. Byers, Brian Ripley and
Scott Chasalow so far.  All suggested the same approach, i.e vectorize the
computations.  This improved the speed by many orders of magnitude.  Here
is a sample solution:


     dif[as.numeric(df[,1])] * df[,2]

Boy, so easy and simple in comparison to my solution!  Brian Ripley and
Scott Chasalow also pointed out that apply coerces its
first argument to matrix form when it is not, and since a factor column of
a data frame will be represented in character mode in a
 matrix it explains why my numeric argument was coerce to character by
apply.

Thanks again for the help, the original question follows,

Gérald Jean
Analyste-conseil (statistiques), Actuariat
télephone            : (418) 835-8839
télecopieur          : (418) 835-5865
courrier électronique: gerald.jean@spgdag.ca

"In God we trust all others must bring data"


---------------------- Envoyée par Gérald Jean/Assurances/SPGDAG/Desjardins
le 99/08/05 16:09 ---------------------------


"Gérald Jean" <Gerald.Jean@spgdag.ca> le 99/08/05 14:37:44
                                                           
                                                           
                                                           
 Pour  s-news@wubios.wustl.edu                             
 :                                                         
                                                           
 cc :  (ccc : Gérald Jean/Assurances/SPGDAG/Desjardins)    
                                                           
                                                           
                                                           
 Objet [S] speeding up function                            
 :                                                         
                                                           






Dear S+ users,

I wrote the following function, see purpose in function's body, and it is
very, very, very slow.  I am running S+ 2000, on a Pentium II, 350Mz, 192M
RAM machine.  Can anyone point out why is it so slow and possible
improvements (my input data frames have over 100K obs.) and also why the
second column of "df", which is numeric to start with, has become character
once each row of "df" is passed in turn to the function inside apply which
forces me to coerce it to numeric again?

Thanks to all

Gérald Jean
Analyste-conseil (statistiques), Actuariat
télephone            : (418) 835-8839
télecopieur          : (418) 835-5865
courrier électronique: gerald.jean@spgdag.ca

"In God we trust all others must bring data"

"correctionformule" <- function (df,diff)
{
# Author : Gerald Jean
# Date   : August 1999
# Purpose: the function will mutiply the second column of "df" by one of
the
#          elements of "diff" depending on the level of the first column
#          factor.
# Parameters:
#  df   : normally a 2 column subset of a data frame, the first column is a
#         factor while the second is a numeric column.
#  diff : one of it's element will multiply the second column of "df"
#         conditionnal on the value of the first column of "df".
assign("diff", diff, frame = 1)
ttt <- apply(df, 1, function(x) {
                                 y <- as.numeric(x[2])
                                 switch(x[1], 'En confort   '= y * diff[1],
                                              'Commodite    '= y * diff[2],
                                              'Restreinte   '= y * diff[3],
                                              'Plein confort'= y *
diff[4])})
ttt
}
Sender: owner-s-news@wubios.wustl.edu
Precedence: bulk
X-loop: s-news


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post