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