[R] entries that match a regexp

Chuck Cleland ccleland at optonline.net
Fri Apr 28 11:35:22 CEST 2006


Albert Vilella wrote:
> Hi all,
> 
> I am trying to filter the element of a df that start with "TF", like
> this:
> 
> alfa =
> c(123221,"TF13124",41243,"TF1234",32414,"TF13124","TF14333",2134123,"TF1234")
> beta =
> c("type_a","type_b","type_a","type_g","type_d","type_a","type_g","type_a","type_g")
> mydf = data.frame(alfa,beta)
> mydf
> tf = mydf[mydf$alfa %in% "TF",]
> 
> Shouldn't the %in% operator to the trick here?

tf <- mydf[grep("^TF", as.character(mydf$alfa)),]

> Thanks in advance,
> 
>     Albert.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

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




More information about the R-help mailing list