[R] Using ifelse and grep

Gabor Grothendieck
Sat, 03 Apr 2010 16:59:23 -0000

# 1
grep returns an index,  not the value unless you use grep(..., value = TRUE).

Easier might be:

# 2
Sample2 <- substr(Sample, 1, 2)
ifelse(Sample2 == "BU", "up", ifelse(Sample2 ==
"BM", "mid", "down"))

or

#3 the following which matches the first 2 characters against the
given list names and return the corresponding list values.

library(gsubfn)
gsubfn("^(..).", list(BU = "up", BD = "down", BM =
"mid"), Sample)

Note that if Sample is a factor rather than character then use
as.character(Sample) in place of Sample in the last line.


On Sat, Apr 3, 2010 at 12:18 PM, Sam Albers <tonightsthenight at
gmail.com> wrote:
> Good Morning,
>
> I am trying to create a new column of character strings based on the first
> two letters in a string in another column. I believe that I need to use
some
> combination of ifelse and grep but I am not totally sure how to combine
> them. I am not totally sure why the command below isn't working.
Obviously
> it isn't finding anything that matches my criteria but I am not sure
why.
> Any ideas on how I might be able to modify this to get to work? Below is
> also a data example of what I would like to achieve with this command.
>
>> section <- ifelse(Sample==grep("^BU",
Sample),"up",
> ifelse(Sample==grep("^BM", Sample),
"mid","down"))
>> section
> ?[1] "down" "down" "down" "down"
"down" "down" "down" "down"
"down" "down"
> [11] "down" "down"
>
> Thanks in advance.
>
> Sam
>
> ?Sample Transmission section ?BU1 0.39353 up ?BU2 0.38778 up ?BU3 0.42645
up
> BM1 0.37510 mid ?BM2 0.5103 mid ?BM3 0.67224 mid ?BD1 0.37482 down ?BD2
> 0.54716 down ?BD3 0.50866 down ?BU1 0.34869 up ?BU2 0.32831 up ?BU3 0.59877
> up ?BM1 0.52518 mid ?BM2 0.94387 mid ?BM3 0.94387 mid ?BD1 0.46872 down
?BD2
> 0.63115 down ?BD3 0.45239 down
> n" "down" "down" "down" "down"
"down" "down"
>
>
>
>
> --
> *****************************************************
> Sam Albers
> Geography Program
> University of Northern British Columbia
> 3333 University Way
> Prince George, British Columbia
> Canada, V2N 4Z9
> phone: 250 960-6777
> *****************************************************
>
> ? ? ? ?[[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.
>