[18876] in s-news-athena

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

[S] speeding up function

daemon@ATHENA.MIT.EDU (=?iso-8859-1?Q?"G=E9rald_Jean"?=)
Thu Aug 5 14:49:55 1999

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

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

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