[R] nested if/else very slow, more efficient ways?
Stefano Calza
calza at med.unibs.it
Tue Oct 24 15:33:56 CEST 2006
If I get it right (data are not well aligned!) these are values in Class column. Right?
Than Class = as.numeric(factor(Class))
should work
Stefano
On Tue, Oct 24, 2006 at 08:20:08AM -0500, Kim Milferstedt wrote:
<Kim>Hi Alex,
<Kim>
<Kim>find below a sample of the input and the desired output of my data.
<Kim>5a ... 5e are indeed the only possible values for the column in
<Kim>question. I would like to replace 5a --> 1, 55b --> 2, ... 5e --> 5.
<Kim>
<Kim>Kim
<Kim>
<Kim>## input ###
<Kim>
<Kim>Image_ID Class Max 1 2 3
<Kim> 4 5 exp
<Kim>060901_1545_17_1_10.tif 5c 0.82329 0.32734 0.12593 0.50405
<Kim>0.03987 0.00282 1.7
<Kim>060902_1570_01_1_01.tif 5a 0.65336 0.59857 0.10341 0.25193
<Kim>0.04384 0.00225 1.7
<Kim>060902_1570_01_1_02.tif 5c 0.57736 0.22884 0.08554 0.65148
<Kim>0.03091 0.00323 1.7
<Kim>060902_1570_01_1_03.tif 5a 0.28857 0.83517 0.02118 0.12374
<Kim>0.018 0.00191 1.7
<Kim>060902_1570_01_1_04.tif 5b 0.58386 0.12772 0.60949 0.19336
<Kim>0.06501 0.00442 1.7
<Kim>060902_1570_01_1_05.tif 5c 0.83419 0.3594 0.08062
<Kim>0.5252 0.03211 0.00267 1.7
<Kim>060902_1570_01_1_06.tif 5c 0.61535 0.27652 0.04082 0.66117
<Kim>0.01933 0.00217 1.7
<Kim>060902_1570_01_1_07.tif 5c 0.22709 0.0969 0.02196 0.86981
<Kim>0.00985 0.00149 1.7
<Kim>060902_1570_01_1_08.tif 5c 0.24596 0.10647 0.02151 0.86051
<Kim>0.01 0.00151 1.7
<Kim>060902_1570_01_1_09.tif 5a 0.92945 0.48802 0.04582 0.41747
<Kim>0.03852 0.01017 1.7
<Kim>060902_1570_01_1_10.tif 5a 0.49169 0.71852 0.03417
<Kim>0.2102 0.03203 0.00508 1.7
<Kim>060902_1570_06_1_01.tif 5b 0.1927 0.05284 0.86286 0.02789
<Kim>0.05556 0.00085 1.7
<Kim>060902_1570_06_1_02.tif 5a 0.12993 0.91879 0.01481 0.04872
<Kim>0.01643 0.00125 1.7
<Kim>
<Kim>## output ###
<Kim>
<Kim>Image_ID Class Max 1 2 3
<Kim> 4 5 exp
<Kim>060901_1545_17_1_10.tif 3 0.82329 0.32734 0.12593 0.50405
<Kim>0.03987 0.00282 1.7
<Kim>060902_1570_01_1_01.tif 1 0.65336 0.59857 0.10341 0.25193
<Kim>0.04384 0.00225 1.7
<Kim>060902_1570_01_1_02.tif 3 0.57736 0.22884 0.08554 0.65148
<Kim>0.03091 0.00323 1.7
<Kim>060902_1570_01_1_03.tif 1 0.28857 0.83517 0.02118 0.12374
<Kim>0.018 0.00191 1.7
<Kim>060902_1570_01_1_04.tif 2 0.58386 0.12772 0.60949 0.19336
<Kim>0.06501 0.00442 1.7
<Kim>060902_1570_01_1_05.tif 3 0.83419 0.3594 0.08062
<Kim>0.5252 0.03211 0.00267 1.7
<Kim>060902_1570_01_1_06.tif 3 0.61535 0.27652 0.04082 0.66117
<Kim>0.01933 0.00217 1.7
<Kim>060902_1570_01_1_07.tif 3 0.22709 0.0969 0.02196 0.86981
<Kim>0.00985 0.00149 1.7
<Kim>060902_1570_01_1_08.tif 3 0.24596 0.10647 0.02151 0.86051
<Kim>0.01 0.00151 1.7
<Kim>060902_1570_01_1_09.tif 1 0.92945 0.48802 0.04582 0.41747
<Kim>0.03852 0.01017 1.7
<Kim>060902_1570_01_1_10.tif 1 0.49169 0.71852 0.03417
<Kim>0.2102 0.03203 0.00508 1.7
<Kim>060902_1570_06_1_01.tif 2 0.1927 0.05284 0.86286 0.02789
<Kim>0.05556 0.00085 1.7
<Kim>060902_1570_06_1_02.tif 1 0.12993 0.91879 0.01481 0.04872
<Kim>0.01643 0.00125 1.7
<Kim>
<Kim>
<Kim>At 17:23 06/10/23, you wrote:
<Kim>>There are a number of ways this might be approached.
<Kim>>
<Kim>>Can you please give a sample of your data, and your desired output?
<Kim>>
<Kim>>Are "5a" ... "5e" the only values that appear in that column, or are
<Kim>>there other values, "4e" for instance, that should stay the same
<Kim>>during your conversion?
<Kim>>
<Kim>>Do you wish to use the numbers 1 to 5 in the processed column in
<Kim>>arithmetic processing, or are they just an enumeration of possible
<Kim>>values?
<Kim>>
<Kim>>While you think about it, I direct your attention to the functions:
<Kim>>
<Kim>>sub
<Kim>>factor
<Kim>>
<Kim>>-Alex
<Kim>>
<Kim>>On 23 Oct 2006, at 23:03, Kim Milferstedt wrote:
<Kim>>
<Kim>>>Hello,
<Kim>>>
<Kim>>>in the data.frame "resultsfuzzy" I would like to replace the
<Kim>>>characters in the second column ("5a", "5b", ... "5e") with numbers
<Kim>>>from 1 to 5. The data.frame has 39150 entries. I seems to work on
<Kim>>>samples that are << nrow(resultsfuzzy) but it takes suspicously long.
<Kim>>>
<Kim>>>Do you have any suggestions how to make the character replacing
<Kim>>>more efficient?
<Kim>>>
<Kim>>>Code:
<Kim>>>
<Kim>>>for (i in 1:nrow(resultsfuzzy))
<Kim>>>{
<Kim>>>if (resultsfuzzy[i,2] == "5a"){resultsfuzzy[i,2] <- 1} else
<Kim>>> if (resultsfuzzy[i,2] == "5b"){resultsfuzzy[i,2] <- 2} else
<Kim>>> if (resultsfuzzy[i,2] == "5c"){resultsfuzzy[i,2] <- 3} else
<Kim>>> if (resultsfuzzy[i,2] == "5d"){resultsfuzzy[i,2] <- 4}
<Kim>>>else
<Kim>>> resultsfuzzy[i,2] <- 5
<Kim>>>}
<Kim>>>
<Kim>>>Thanks,
<Kim>>>
<Kim>>>Kim
<Kim>>>
<Kim>>>version
<Kim>>>
<Kim>>>platform i386-pc-mingw32
<Kim>>>arch i386
<Kim>>>os mingw32
<Kim>>>system i386, mingw32
<Kim>>>status
<Kim>>>major 2
<Kim>>>minor 2.1
<Kim>>>year 2005
<Kim>>>month 12
<Kim>>>day 20
<Kim>>>svn rev 36812
<Kim>>>language R
<Kim>>>
<Kim>>>__________________________________________
<Kim>>>
<Kim>>>Kim Milferstedt
<Kim>>>University of Illinois at Urbana-Champaign
<Kim>>>Department of Civil and Environmental Engineering
<Kim>>>4125 Newmark Civil Engineering Laboratory
<Kim>>>205 North Mathews Avenue MC-250
<Kim>>>Urbana, IL 61801
<Kim>>>USA
<Kim>>>phone: (001) 217 333-9663
<Kim>>>fax: (001) 217 333-6968
<Kim>>>email: milferst a uiuc.edu
<Kim>>>http://cee.uiuc.edu/research/morgenroth
<Kim>>>
<Kim>>>______________________________________________
<Kim>>>R-help a stat.math.ethz.ch mailing list
<Kim>>>https://stat.ethz.ch/mailman/listinfo/r-help
<Kim>>>PLEASE do read the posting guide http://www.R-project.org/posting- guide.html
<Kim>>>and provide commented, minimal, self-contained, reproducible code.
<Kim>
<Kim>______________________________________________
<Kim>R-help a stat.math.ethz.ch mailing list
<Kim>https://stat.ethz.ch/mailman/listinfo/r-help
<Kim>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
<Kim>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list