[R] subset by multiple letters condition

chalabi.elahe at yahoo.de chalabi.elahe at yahoo.de
Sat Apr 23 18:09:21 CEST 2016


Thanks Jean, Does anyone know how to set these [hast1] and [hast2] as the colors of a plot?  


On Friday, April 22, 2016 7:39 AM, "Adams, Jean" <jvadams at usgs.gov> wrote:



You can use the grepl() function to give you logicals for each criterion, then combine them as needed.  For example:

# example version of Command
Command <- paste0("_localize_", c("PD","t2","t1_seq", "abc", "xyz", "PD_t1"))

hasPD <- grepl("PD", Command, fixed=TRUE)
hast1 <- grepl("t1", Command, fixed=TRUE)
hast2 <- grepl("t2", Command, fixed=TRUE)

> Command[hast1]
[1] "_localize_t1_seq" "_localize_PD_t1" 

> Command[hasPD]
[1] "_localize_PD"    "_localize_PD_t1"

> Command[hast1 & hasPD]
[1] "_localize_PD_t1"

Jean


On Fri, Apr 22, 2016 at 8:42 AM, ch.elahe via R-help <r-help at r-project.org> wrote:


>Hi all,
>
>I have a data frame df and I want to do subset based on several conditions of letters of the names in Command.1)if the names contain PD 2)if the names contain t1 3)if the names contain t2 4)if the names contain t1 and PD 5)if the names contain t2 and PD 6)otherwise the names would be unknown. I don't know how to use grep for all these conditions.
>
>   'data.frame': 36919 obs. of 162 variables
>   $TE                :int 38,41,11,52,48,75,.....
>   $Command           :factor W/2229 levels "_localize_PD","_localize_tre_t2","_localize_t1_seq",...
>
>
>Thanks for any help
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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