[R] Finding/identifying a value within a factor

Peter Ehlers ehlers at ucalgary.ca
Mon Jul 25 21:32:31 CEST 2011


On 2011-07-25 10:51, "Dénes TÓTH" wrote:
>
> Hi,
>
> you provided a character vector as an example. I guess you meant something
> like:
> x<- factor(c("1","2","3","4","<1"))
>
> # You can identify those elements with an "<" by ?grep or ?grepl:
> indices<- grep("<",as.character(x))
>
> # You can transform those elements by ?as.numeric
> as.numeric(x[indices])

I fail to see what this is supposed to accomplish. It gives

   as.numeric(x[indices])
   #[1] 1

Try changing x to

   x <- factor(c("1","2","3","4","<8"))

You'll still get

   as.numeric(x[indices])
   #[1] 1

?factor explains this.

Better just stick with a character vector to begin with.

Peter Ehlers

>
> HTH,
>    Denes
>
>
>> Hi all,
>>
>> I'm trying to identify a particular digit or value within a vector of
>> factors. Specifically, this is environmental data where in some cases the
>> minimum value reported is "<" a particular number (and I want to
>> manipulate
>> only these). For example:
>>
>>   x<-c("1","2","3","4","<1")
>>
>> For a dataset that is hundreds or thousands of lines long, I'd like to
>> find
>> or identify only those that have a "<" symbol (R automatically stores the
>> entire vector in factor format due to these symbols when it imports the
>> data-I don't mind converting if necessary). Eventually, I'd like to divide
>> the number in half for these cases, but I think I have that coding lined
>> up
>> once I can just identify them from the stew.
>>
>> I've exhausted help and net resources so far...
>>
>> Thanks,
>> Ryan
>>
>>
>> --
>>
>> Ryan Utz, Ph.D.
>> Aquatic Ecologist/STREON Scientist
>> National Ecological Observatory Network
>>
>> Home/Cell: (724) 272-7769
>> Work: (720) 746-4844 ext. 2488
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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