[R] nested if/else very slow, more efficient ways?

From: Kim Milferstedt <milferst_at_uiuc.edu>
Date: Mon 23 Oct 2006 - 22:03:47 GMT


Hello,

in the data.frame "resultsfuzzy" I would like to replace the characters in the second column ("5a", "5b", ... "5e") with numbers from 1 to 5. The data.frame has 39150 entries. I seems to work on samples that are << nrow(resultsfuzzy) but it takes suspicously long.

Do you have any suggestions how to make the character replacing more efficient?

Code:

for (i in 1:nrow(resultsfuzzy))
{
if (resultsfuzzy[i,2] == "5a"){resultsfuzzy[i,2] <- 1} else

     if (resultsfuzzy[i,2] == "5b"){resultsfuzzy[i,2] <- 2} else
         if (resultsfuzzy[i,2] == "5c"){resultsfuzzy[i,2] <- 3} else
             if (resultsfuzzy[i,2] == "5d"){resultsfuzzy[i,2] <- 4} else
                 resultsfuzzy[i,2] <- 5

}

Thanks,

Kim

version

platform i386-pc-mingw32

arch     i386
os       mingw32

system i386, mingw32
status
major 2
minor 2.1
year 2005
month 12
day 20
svn rev 36812
language R

Kim Milferstedt
University of Illinois at Urbana-Champaign Department of Civil and Environmental Engineering 4125 Newmark Civil Engineering Laboratory 205 North Mathews Avenue MC-250
Urbana, IL 61801
USA
phone: (001) 217 333-9663
fax: (001) 217 333-6968
email: milferst@uiuc.edu
http://cee.uiuc.edu/research/morgenroth



R-help@stat.math.ethz.ch 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. Received on Tue Oct 24 12:26:05 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 24 Oct 2006 - 05:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.