[R] problem with 'which' and strings

Chrischizinski chris.chizinski at gmail.com
Wed Feb 11 15:20:33 CET 2009


I have written a function that goes through a database and calculates various
metric scores and allocates them to a data set.  For 400 of the 500 sites
that I calculate these metrics for works fine and allocates the scores into
the appropriate column.  For some reason, some sites I run into the below
problem:

Here is what I am doing:

> names(orig.metric)
 [1] "BenInsect"              "CountofTaxa"            "Darter"                
 [4] "DomTwoPct"              "FishDELTPct"            "Minnows-Tolerant"      
 [7] "NumberPer100m-Tolerant" "Sensitive"              "SLithopPct"            
[10] "TolerantPct"            "Omnivore"               "PiscivorePct"          
[13] "Headwater-Tolerant"     "Insect-Tolerant"        "Wetland-Tolerant"      
[16] "Piscivore"              "DarterSculpNot"         "OmnivorePct"           
[19] "SlithopPct" 

For this given site the metrics that are calculated are
XXX.table$Metric.name
 [1] "CountofTaxa"            "DarterSculpNot"         "FishDELTPct"           
 [4] "Insect-Tolerant"        "NumberPer100m-Tolerant" "PiscivoreNumber"       
 [7] "Sensitive"              "SLithopPct"             "TolerantPct"           
[10] "Wetland-Tolerant" 

I then try to use 'which' to determine the appropriate columns to allocate
each metric into
no<- which(names(orig.metric)==XXX.table$Metric.name)

and this gives me

> no
[1] 14

So when I use 
orig.metric.scores[no]<- IBI.table.0$IBI.score

I get this error:
Error in `[<-.data.frame`(`*tmp*`, no, value = c(2, 5, 10, 2, 10, 0, 0,  : 
 replacement has 10 rows, data has 1

I know why the error is being generated but I do not know why it will not
properly identify which columns to allocate the metrics into.  I have tried
many things, including setting options(stringsAsFactors = FALSE) in my
Rprofile.site.  

Where am I going wrong here?  Any suggestions or work arounds would help me
greatly.
-- 
View this message in context: http://www.nabble.com/problem-with-%27which%27-and-strings-tp21955970p21955970.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list