Example data frame:
a = c("Alpha", "Beta", "Gamma", "Beeta",
"Alpha", "beta")
b = c(1:6)
example = data.frame("Title" = a, "Vals" = b)
> example
Title Vals
1 Alpha 1
2 Beta 2
3 Gamma 3
4 Beeta 4
5 Alpha 5
6 beta 6>
I would like to be able to get a new data frame from this data frame
containing only rows that match a certain string. In this case it
could for instance be the string "eta". I have tried various ways of
using agrep and grep, but so far I have not found anything that
worked.
Thankyou in advance for your help!
Karin
--
Karin Lagesen, PhD student
karin.lagesen at medisin.uio.no
http://folk.uio.no/karinlag
On 1/21/2008 5:18 AM, Karin Lagesen wrote:> Example data frame: > > > a = c("Alpha", "Beta", "Gamma", "Beeta", "Alpha", "beta") > b = c(1:6) > example = data.frame("Title" = a, "Vals" = b) > > >> example > Title Vals > 1 Alpha 1 > 2 Beta 2 > 3 Gamma 3 > 4 Beeta 4 > 5 Alpha 5 > 6 beta 6 > > I would like to be able to get a new data frame from this data frame > containing only rows that match a certain string. In this case it > could for instance be the string "eta". I have tried various ways of > using agrep and grep, but so far I have not found anything that > worked. > > Thankyou in advance for your help!a <- c("Alpha", "Beta", "Gamma", "Beeta", "Alpha", "beta") b <- c(1:6) df <- data.frame(Title = a, Vals = b) df[grep("eta", df$Title),] Title Vals 2 Beta 2 4 Beeta 4 6 beta 6> Karin-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Richard.Cotton at hsl.gov.uk
2008-Jan-21 11:10 UTC
[R] subsetting a data frame using string matching
> a = c("Alpha", "Beta", "Gamma", "Beeta", "Alpha", "beta") > b = c(1:6) > example = data.frame("Title" = a, "Vals" = b) > > > > example > Title Vals > 1 Alpha 1 > 2 Beta 2 > 3 Gamma 3 > 4 Beeta 4 > 5 Alpha 5 > 6 beta 6 > > > > I would like to be able to get a new data frame from this data frame > containing only rows that match a certain string. In this case it > could for instance be the string "eta". I have tried various ways of > using agrep and grep, but so far I have not found anything that > worked.Sounds like you were nearly there. rows.to.keep <- grep("eta", example$Title) subdata <- example[rows.to.keep,] Regards, Richie. Mathematical Sciences Unit HSL "Statistics are like a lamp-post to a drunken man - more for leaning on than illumination." David Brent, The Office. ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}