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

Petr Pikal petr.pikal at precheza.cz
Tue Oct 24 15:39:38 CEST 2006


Hi

so probably easiest approach is

input$Class<-as.numeric(input$Class)

if input$Class is factor, what can be tested e.g. by 

str(input)

HTH
Petr


On 24 Oct 2006 at 8:20, Kim Milferstedt wrote:

Date sent:      	Tue, 24 Oct 2006 08:20:08 -0500
To:             	r-help at stat.math.ethz.ch
From:           	Kim Milferstedt <milferst at uiuc.edu>
Subject:        	Re: [R] nested if/else very slow, more efficient ways?

> Hi Alex,
> 
> find below a sample of the input and the desired output of my data. 5a
> ... 5e are indeed the only possible values for the column in question.
> I would like to replace 5a --> 1, 55b --> 2, ... 5e --> 5.
> 
> Kim
> 
> ## input ###
> 
> Image_ID                        Class   Max     1       2       3 
>    4       5       exp
> 060901_1545_17_1_10.tif 5c      0.82329 0.32734 0.12593 0.50405 
> 0.03987 0.00282 1.7
> 060902_1570_01_1_01.tif 5a      0.65336 0.59857 0.10341 0.25193 
> 0.04384 0.00225 1.7
> 060902_1570_01_1_02.tif 5c      0.57736 0.22884 0.08554 0.65148 
> 0.03091 0.00323 1.7
> 060902_1570_01_1_03.tif 5a      0.28857 0.83517 0.02118 0.12374 
> 0.018   0.00191 1.7
> 060902_1570_01_1_04.tif 5b      0.58386 0.12772 0.60949 0.19336 
> 0.06501 0.00442 1.7
> 060902_1570_01_1_05.tif 5c      0.83419 0.3594  0.08062 
> 0.5252  0.03211 0.00267 1.7
> 060902_1570_01_1_06.tif 5c      0.61535 0.27652 0.04082 0.66117 
> 0.01933 0.00217 1.7
> 060902_1570_01_1_07.tif 5c      0.22709 0.0969  0.02196 0.86981 
> 0.00985 0.00149 1.7
> 060902_1570_01_1_08.tif 5c      0.24596 0.10647 0.02151 0.86051 
> 0.01    0.00151 1.7
> 060902_1570_01_1_09.tif 5a      0.92945 0.48802 0.04582 0.41747 
> 0.03852 0.01017 1.7
> 060902_1570_01_1_10.tif 5a      0.49169 0.71852 0.03417 
> 0.2102  0.03203 0.00508 1.7
> 060902_1570_06_1_01.tif 5b      0.1927  0.05284 0.86286 0.02789 
> 0.05556 0.00085 1.7
> 060902_1570_06_1_02.tif 5a      0.12993 0.91879 0.01481 0.04872 
> 0.01643 0.00125 1.7
> 
> ## output ###
> 
> Image_ID                        Class   Max     1       2       3 
>    4       5       exp
> 060901_1545_17_1_10.tif 3       0.82329 0.32734 0.12593 0.50405 
> 0.03987 0.00282 1.7
> 060902_1570_01_1_01.tif 1       0.65336 0.59857 0.10341 0.25193 
> 0.04384 0.00225 1.7
> 060902_1570_01_1_02.tif 3       0.57736 0.22884 0.08554 0.65148 
> 0.03091 0.00323 1.7
> 060902_1570_01_1_03.tif 1       0.28857 0.83517 0.02118 0.12374 
> 0.018   0.00191 1.7
> 060902_1570_01_1_04.tif 2       0.58386 0.12772 0.60949 0.19336 
> 0.06501 0.00442 1.7
> 060902_1570_01_1_05.tif 3       0.83419 0.3594  0.08062 
> 0.5252  0.03211 0.00267 1.7
> 060902_1570_01_1_06.tif 3       0.61535 0.27652 0.04082 0.66117 
> 0.01933 0.00217 1.7
> 060902_1570_01_1_07.tif 3       0.22709 0.0969  0.02196 0.86981 
> 0.00985 0.00149 1.7
> 060902_1570_01_1_08.tif 3       0.24596 0.10647 0.02151 0.86051 
> 0.01    0.00151 1.7
> 060902_1570_01_1_09.tif 1       0.92945 0.48802 0.04582 0.41747 
> 0.03852 0.01017 1.7
> 060902_1570_01_1_10.tif 1       0.49169 0.71852 0.03417 
> 0.2102  0.03203 0.00508 1.7
> 060902_1570_06_1_01.tif 2       0.1927  0.05284 0.86286 0.02789 
> 0.05556 0.00085 1.7
> 060902_1570_06_1_02.tif 1       0.12993 0.91879 0.01481 0.04872 
> 0.01643 0.00125 1.7
> 
> 
> At 17:23 06/10/23, you wrote:
> >There are a number of ways this might be approached.
> >
> >Can you please give a sample of your data, and your desired output?
> >
> >Are "5a" ... "5e" the only values that appear in that column, or are
> >there other values, "4e" for instance, that should stay the same
> >during your conversion?
> >
> >Do you wish to use the numbers 1 to 5 in the processed column in
> >arithmetic processing, or are they just an enumeration of possible
> >values?
> >
> >While you think about it, I direct your attention to the functions:
> >
> >sub
> >factor
> >
> >-Alex
> >
> >On 23 Oct 2006, at 23:03, 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?
> >>
> >>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 at uiuc.edu
> >>http://cee.uiuc.edu/research/morgenroth
> >>
> >>______________________________________________
> >>R-help at 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 at 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.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list