[R] problem with 'which' and strings
jim holtman
jholtman at gmail.com
Wed Feb 11 17:11:15 CET 2009
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:
>
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list