Hi, I have a good grasp of grep() and gsub() for finding and extracting character strings. However, I cannot figure out how to use a search term that is stored in a variable when the search string is more complex. #Say I have a string, and want to know whether the last name "Jannings" is in the string. This is done by names=c("Emil Jannings") grep("Emil",names) #Yet, I need to store the search terms in a variable, which works for the very simple example search.term="Emil" grep(search.term,names) #but I cannot get it to work for the more difficult example in which I want to do something like grep(^search.term,names) grep("^search.term",names) grep("^"search.term,names) #Implying that the search term must be the first part of the string that is being searched #Ultimately, I need to to loop over several strings stored in search.term, for example, names=c("Emil Jannings","Charles Chaplin","Katherine Hepburn","Meryl Streep") search.term=c("Emil","Meryl") for(i in 1:length(names)){ print(grep(^search.term[i],names)) } So the questions I have are two. 1. How do I concatenate terms that I would normally quote (like "^") with variables that contain search terms and that normally would not be quoted? 2. How do I run this over indices of the variable that contains the search terms? I greatly appreciate any help, Daniel -- View this message in context: http://r.789695.n4.nabble.com/grep-with-search-terms-defined-by-a-variable-tp2311294p2311294.html Sent from the R help mailing list archive at Nabble.com.
Hi Daniel, What about this? ############# my.names <- c("Emil Jannings", "Charles Chaplin", "Katherine Hepburn", "Meryl Streep") search.term <- c("Emil", "Meryl") for(i in 1:length(search.term)){ print(grep(paste("^", search.term, sep="")[i], my.names)) } ############## Cheers, Josh On Mon, Aug 2, 2010 at 9:05 PM, Daniel Malter <daniel at umd.edu> wrote:> > Hi, I have a good grasp of grep() and gsub() for finding and extracting > character strings. However, I cannot figure out how to use a search term > that is stored in a variable when the search string is more complex. > > #Say I have a string, and want to know whether the last name "Jannings" is > in the string. This is done by > > names=c("Emil Jannings") > grep("Emil",names) > > #Yet, I need to store the search terms in a variable, which works for the > very simple example > > search.term="Emil" > grep(search.term,names) > > #but I cannot get it to work for the more difficult example in which I want > to do something like > > grep(^search.term,names) > grep("^search.term",names) > grep("^"search.term,names) > > #Implying that the search term must be the first part of the string that is > being searched > > #Ultimately, I need to to loop over several strings stored in search.term, > for example, > > names=c("Emil Jannings","Charles Chaplin","Katherine Hepburn","Meryl > Streep") > search.term=c("Emil","Meryl") > > for(i in 1:length(names)){ > ? ? print(grep(^search.term[i],names)) > } > > So the questions I have are two. 1. How do I concatenate terms that I would > normally quote (like "^") with variables that contain search terms and that > normally would not be quoted? 2. How do I run this over indices of the > variable that contains the search terms? > > I greatly appreciate any help, > Daniel > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/grep-with-search-terms-defined-by-a-variable-tp2311294p2311294.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
A beauty. Works a charm. Many many thanks. Daniel -- View this message in context: http://r.789695.n4.nabble.com/grep-with-search-terms-defined-by-a-variable-tp2311294p2311307.html Sent from the R help mailing list archive at Nabble.com.
Daniel, If you want to search for each term at the beginning of a sting, using the regular expression construct '^', you might use the following> search.terms <- c("Emil", "Meryl") > names <- c("Emil Jannings",+ "Charles Chaplin", + "Katherine Hepburn", + "Meryl Streep")> for(term in search.terms) {+ print(grep(paste("^",term,sep=""),names)) + } [1] 1 [1] 4 -Matt On Tue, 2010-08-03 at 00:05 -0400, Daniel Malter wrote:> Hi, I have a good grasp of grep() and gsub() for finding and extracting > character strings. However, I cannot figure out how to use a search term > that is stored in a variable when the search string is more complex. > > #Say I have a string, and want to know whether the last name "Jannings" is > in the string. This is done by > > names=c("Emil Jannings") > grep("Emil",names) > > #Yet, I need to store the search terms in a variable, which works for the > very simple example > > search.term="Emil" > grep(search.term,names) > > #but I cannot get it to work for the more difficult example in which I want > to do something like > > grep(^search.term,names) > grep("^search.term",names) > grep("^"search.term,names) > > #Implying that the search term must be the first part of the string that is > being searched > > #Ultimately, I need to to loop over several strings stored in search.term, > for example, > > names=c("Emil Jannings","Charles Chaplin","Katherine Hepburn","Meryl > Streep") > search.term=c("Emil","Meryl") > > for(i in 1:length(names)){ > print(grep(^search.term[i],names)) > } > > So the questions I have are two. 1. How do I concatenate terms that I would > normally quote (like "^") with variables that contain search terms and that > normally would not be quoted? 2. How do I run this over indices of the > variable that contains the search terms? > > I greatly appreciate any help, > Daniel > > > >-- Matthew S. Shotwell Graduate Student Division of Biostatistics and Epidemiology Medical University of South Carolina
On Aug 3, 2010, at 12:05 AM, Daniel Malter wrote:> > Hi, I have a good grasp of grep() and gsub() for finding and > extracting > character strings. However, I cannot figure out how to use a search > term > that is stored in a variable when the search string is more complex. > > #Say I have a string, and want to know whether the last name > "Jannings" is > in the string. This is done by > > names=c("Emil Jannings") > grep("Emil",names) > > #Yet, I need to store the search terms in a variable, which works > for the > very simple example > > search.term="Emil" > grep(search.term,names) > > #but I cannot get it to work for the more difficult example in which > I want > to do something like > > grep(^search.term,names) > grep("^search.term",names) > grep("^"search.term,names) > > #Implying that the search term must be the first part of the string > that is > being searched > > #Ultimately, I need to to loop over several strings stored in > search.term, > for example, > > names=c("Emil Jannings","Charles Chaplin","Katherine Hepburn","Meryl > Streep") > search.term=c("Emil","Meryl") > > for(i in 1:length(names)){ > print(grep(^search.term[i],names)) > } > > So the questions I have are two. 1. How do I concatenate terms that > I would > normally quote (like "^") with variables that contain search terms > and that > normally would not be quoted? 2. How do I run this over indices of the > variable that contains the search terms?You have gotten answers to both but for the second question there exists a loopless alternative. This is implicitly a logical OR operations so you could construct a single grep expression with the collaps argument to paste: > grep( paste("^", search.term, sep="", collapse="|"), names) [1] 1 4 It may not be quite as flexible because it would not deliver the match information in a sequence that aligns with the order of the search terms. -- David.> > I greatly appreciate any help, > Daniel > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/grep-with-search-terms-defined-by-a-variable-tp2311294p2311294.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
You might also find match and pmatch of use here. ------------ On Aug 3, 2010, at 12:05 AM, Daniel Malter wrote:> > Hi, I have a good grasp of grep() and gsub() for finding and > extracting > character strings. However, I cannot figure out how to use a search > term > that is stored in a variable when the search string is more complex. > > #Say I have a string, and want to know whether the last name > "Jannings" is > in the string. This is done by > > names=c("Emil Jannings") > grep("Emil",names) > > #Yet, I need to store the search terms in a variable, which works > for the > very simple example > > search.term="Emil" > grep(search.term,names) > > #but I cannot get it to work for the more difficult example in which > I want > to do something like > > grep(^search.term,names) > grep("^search.term",names) > grep("^"search.term,names) > > #Implying that the search term must be the first part of the string > that is > being searched > > #Ultimately, I need to to loop over several strings stored in > search.term, > for example, > > names=c("Emil Jannings","Charles Chaplin","Katherine Hepburn","Meryl > Streep") > search.term=c("Emil","Meryl") > > for(i in 1:length(names)){ > print(grep(^search.term[i],names)) > } > > So the questions I have are two. 1. How do I concatenate terms that > I would > normally quote (like "^") with variables that contain search terms > and that > normally would not be quoted? 2. How do I run this over indices of the > variable that contains the search terms?You have gotten answers to both but for the second question there exists a loopless alternative. This is implicitly a logical OR operations so you could construct a single grep expression with the collaps argument to paste: > grep( paste("^", search.term, sep="", collapse="|"), names) [1] 1 4 It may not be quite as flexible because it would not deliver the match information in a sequence that aligns with the order of the search terms. -- David.> > I greatly appreciate any help, > Daniel > >********************************************************************** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager (it.support at cancer.ucl.ac.uk). **********************************************************************