Hi, How can I use a substring match as a condition in a subset command? Sth like this: subset(input, field1=="blah1" & field2=="blah2") # but now with substring match in field2 subset(input, field1=="blah1" & field3 *substringmatch* "blah3") I've tried with gsub, but it won't work: subset(input, field1=="blah1" & gsub("blah3","",input$field3)) [[alternative HTML version deleted]]
Something like this may be close to what you want: subset(input, field1=="blah1" & !is.na(charmatch("blah3",input$field3))) --- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Albert Vilella Sent: 29 May 2008 11:38 To: r-help at r-project.org Subject: [R] how to use substring match as condition? Hi, How can I use a substring match as a condition in a subset command? Sth like this: subset(input, field1=="blah1" & field2=="blah2") # but now with substring match in field2 subset(input, field1=="blah1" & field3 *substringmatch* "blah3") I've tried with gsub, but it won't work: subset(input, field1=="blah1" & gsub("blah3","",input$field3)) [[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.
On Thu, 29 May 2008, Albert Vilella wrote:> Hi, > > How can I use a substring match as a condition in a subset command? >Perhaps subset(input, field1=="blah1" & regexpr("blah2",field3) != -1 ) ?? Study in example(gsub) the regexpr example and in ?gsub the 'Value' section as it pertains to regexpr. HTH, Chuck> Sth like this: > > subset(input, field1=="blah1" & field2=="blah2") # but now with substring > match in field2 > > subset(input, field1=="blah1" & field3 *substringmatch* "blah3") > > I've tried with gsub, but it won't work: > > subset(input, field1=="blah1" & gsub("blah3","",input$field3)) > > [[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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Maybe Matching Threads
- I need "validates_presence_of" help
- [Bug 1582] New: rsync dry run cannot find missing folders, contradicts actual run.
- Weird behaviour of tab characters in a string in R (vs Python)
- How to add virtual fields in model
- How to use R to perform prediction based on history data