[R] Using ifelse and grep
William Dunlap
wdunlap at tibco.com
Sat Apr 3 18:55:01 CEST 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Sam Albers
> Sent: Saturday, April 03, 2010 9:18 AM
> To: r-help at r-project.org
> Subject: [R] Using ifelse and grep
>
> 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"
I'm not sure what Sample contains, but if you break this
nested set of functions calls down you can see what the problem is:
grep() returns the positions of strings that match the pattern.
E.g., if you have
Sample <- c("BU1", "BU2", "BM1", "BD1", "BU3")
then grep("^BU", Sample) returns c(1,2,5) and
Sample==c(1,2,5) doesn't make much sense.
If you instead grepl in a subscript, as in
section <- character(length(Sample))
section[grepl("^BU", Sample)] <- "up"
section[grepl("^BM", Sample)] <- "mid"
section[grepl("^BD", Sample)] <- "down"
then section is c("up","up","mid","down","up").
There are lots of ways to make this mapping, especially
for such a simple set of patterns (they don't overlap
and they are all the initial 2 characters of a string).
E.g.,
map <- c(BU="up", BM="mid", BD="down")
section <- unname(map[substring(Sample, 1, 2)])
or
from <- c("BU", "BM", "BD")
to <- c("up", "mid", "down")
section <- to[match(substring(Sample,1,2), from)]
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
> 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.
>
More information about the R-help
mailing list