Skip to content

problem with 'which' and strings

5 messages · David Winsemius, jim holtman, Chrischizinski

#
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:
[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
[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.
#
Looks like you might be using vector addressing on a non-existent  
object.

You have identified a column number, 14,  to use. If you wanted to put  
the XXX.table$Metric.name vector into a column of orig.metric.scores   
(and it already existed with 10 rows), then you would would use matrix  
style addressing;

  
    
#
That would allocate, the vector down the column, correct? I should have
specified this clearer.   I want to put each metric value into the
appropriate column (as designated by the metric name ) in a single row.  My
data set SCORES.combined has the column headings for all potential metrics
that can be calculated in the database (as shown with names(orig.metric) ).
For example:

SCORES.combined[1,1:6]
	  'BenInsect'	CountofTaxa	Darter	DomTwoPct	     FishDELTPct
Minnows-Tolerant	
06SC048	0	              10	              0	                7	           10	                    
0	

whereas the next site would not necessarily have the same metrics calculated
but would have the metric values allocated into the next row based on the
column headings.
David Winsemius wrote:

  
    
#
You probably want to use either '%in%' or 'match'.  The result of 14
that you are getting is due to recycling of the shorter vector and
matching at the 14th position ("Insect-Tolerant").

On Wed, Feb 11, 2009 at 9:20 AM, Chrischizinski
<chris.chizinski at gmail.com> wrote: