[R] Using ifelse and grep

Sam Albers
Sat, 03 Apr 2010 16:18:09 -0000

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]]