[R] problem with 'which' and strings

David Winsemius dwinsemius at comcast.net
Wed Feb 11 15:47:01 CET 2009


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;

> orig.metric.scores[,no]<- IBI.table.0$IBI.score

-- 
David Winsemius

On Feb 11, 2009, at 9:20 AM, Chrischizinski wrote:

>
> 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.
>
> ______________________________________________
> R-help at r-project.org 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.




More information about the R-help mailing list