Hi all, I have one factor variable in my df and I want to extract the names from it which contain both "t2" and "pd": 'data.frame': 36919 obs. of 162 variables $TE :int 38,41,11,52,48,75,..... $TR :int 100,210,548,546,..... $Command :factor W/2229 levels "_localize_PD","_localize_tre_t2","_abdomen_t1_seq","knee_pd_t1_localize","pd_local_abdomen_t2"... I have tried this but I did not get result: t2pd=subset(df,grepl("t2",Command) & grepl("pd",Command)) does anyone know how to apply AND in grepl? Thanks Elahe
Your code looks fine to me. What did t2pd look like? I tried reproducing the problem in R-3.2.4(Revised) and everything worked (although the output of str() looked a bit different - perhaps you have an old version of R)> df <- data.frame(TE=1:10, TR=101:110,Command=c("pd_local_abdomen_t2","knee_pd_t1_localize","PD_localize_tre_t2","t2_localize_PD")[rep(1:4,len=10)])> str(df)'data.frame': 10 obs. of 3 variables: $ TE : int 1 2 3 4 5 6 7 8 9 10 $ TR : int 101 102 103 104 105 106 107 108 109 110 $ Command: Factor w/ 4 levels "knee_pd_t1_localize",..: 2 1 3 4 2 1 3 4 2 1> subset(df,grepl("t2",Command) & grepl("pd",Command))TE TR Command 1 1 101 pd_local_abdomen_t2 5 5 105 pd_local_abdomen_t2 9 9 109 pd_local_abdomen_t2> subset(df,grepl("t2",Command,ignore.case=TRUE) &grepl("pd",Command,ignore.case=TRUE)) TE TR Command 1 1 101 pd_local_abdomen_t2 3 3 103 PD_localize_tre_t2 4 4 104 t2_localize_PD 5 5 105 pd_local_abdomen_t2 7 7 107 PD_localize_tre_t2 8 8 108 t2_localize_PD 9 9 109 pd_local_abdomen_t2 Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Apr 30, 2016 at 2:38 PM, ch.elahe via R-help <r-help at r-project.org> wrote:> Hi all, > > I have one factor variable in my df and I want to extract the names from > it which contain both "t2" and "pd": > > 'data.frame': 36919 obs. of 162 variables > $TE :int 38,41,11,52,48,75,..... > $TR :int 100,210,548,546,..... > $Command :factor W/2229 levels > "_localize_PD","_localize_tre_t2","_abdomen_t1_seq","knee_pd_t1_localize","pd_local_abdomen_t2"... > > I have tried this but I did not get result: > > t2pd=subset(df,grepl("t2",Command) & grepl("pd",Command)) > > > does anyone know how to apply AND in grepl? > > Thanks > Elahe > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
subset(df,grepl("t2|pd",x$Command)) On Sat, Apr 30, 2016 at 2:38 PM, ch.elahe via R-help <r-help at r-project.org> wrote:> Hi all, > > I have one factor variable in my df and I want to extract the names from > it which contain both "t2" and "pd": > > 'data.frame': 36919 obs. of 162 variables > $TE :int 38,41,11,52,48,75,..... > $TR :int 100,210,548,546,..... > $Command :factor W/2229 levels > "_localize_PD","_localize_tre_t2","_abdomen_t1_seq","knee_pd_t1_localize","pd_local_abdomen_t2"... > > I have tried this but I did not get result: > > t2pd=subset(df,grepl("t2",Command) & grepl("pd",Command)) > > > does anyone know how to apply AND in grepl? > > Thanks > Elahe > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Actually not sure my previous answer does what you wanted. Using your approach: t2pd=subset(df,grepl("t2",df$Command) & grepl("pd",df$Command)) Should work. I think the regex pattern you are looking for is: Subset(df,grepl("(.* t2.*pd.* )|(.* pd.* t2.*)",df$Command) On Sat, Apr 30, 2016, 7:07 PM Tom Wright <tom at maladmin.com> wrote:> subset(df,grepl("t2|pd",x$Command)) > > > On Sat, Apr 30, 2016 at 2:38 PM, ch.elahe via R-help <r-help at r-project.org > > wrote: > >> Hi all, >> >> I have one factor variable in my df and I want to extract the names from >> it which contain both "t2" and "pd": >> >> 'data.frame': 36919 obs. of 162 variables >> $TE :int 38,41,11,52,48,75,..... >> $TR :int 100,210,548,546,..... >> $Command :factor W/2229 levels >> "_localize_PD","_localize_tre_t2","_abdomen_t1_seq","knee_pd_t1_localize","pd_local_abdomen_t2"... >> >> I have tried this but I did not get result: >> >> t2pd=subset(df,grepl("t2",Command) & grepl("pd",Command)) >> >> >> does anyone know how to apply AND in grepl? >> >> Thanks >> Elahe >> >> ______________________________________________ >> 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. >> >[[alternative HTML version deleted]]