On 10/23/2006 6:03 PM, Kim Milferstedt wrote: > 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?
I don't know if nested if/else is inefficient, but indexing of data.frames certainly is. You would do a lot better with ifelse() on the whole column. For example, if column 2 is named B, this would be much faster: resultsfuzzy$B <- ifelse(resultsfuzzy$B == "5a", 1, ifelse(resultsfuzzy$B == "5b", 2, ... )) (where you'll have to fill in the ... yourself in the obvious way. But an even faster way for your particular problem would be resultsfuzzy$B <- match(resultsfuzzy$B, c("5a", "5b", "5c", "5d", "5e")) Duncan Murdoch > > 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: [EMAIL PROTECTED] > 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. ______________________________________________ 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.