2017-03-09 66 views
0

我试图在R中编写一个函数来读取我的两个单词,然后打印出包含这两个单词的所有字符串。R:用户自定义函数来查找包含两个字符串的字符串

我有一个名为x的数据框,第一列是我需要从中搜索字符串的地方。

我写道:

findcontain <- function{ 
Clue1 <- readline(prompt= "Enter the first Clue.") 
Clue2 <- readline(prompt= "Enter the second Clue.") 
Clues <- paste(Clue1, "&", Clue2, sep="") 
grep(Clues, x[1,])} 

这是不工作...我错过了什么?请帮忙。

+1

请阅读[如何提出一个很好的问题(HTTP的信息:// stackoverflow.com/help/how-to-ask)以及如何给出[可重现的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/ 5963610)。这会让其他人更容易帮助你。 – Jaap

回答

0

取决于你到底想要什么,你可能需要去适应这个有点,但是这可能会帮助您:

findcontain <- function(x){ 
    Clue1 <- readline(prompt= "Enter the first Clue.") 
    Clue2 <- readline(prompt= "Enter the second Clue.") 

intersect(grep(Clue1, x$col1) , grep(Clue2, x$col1)) } 

x<-data.frame(col1=c("this is an example","another example","bla","blabla","tree", 
"house","house and tree")) 

findcontain(x) 
+0

谢谢!这解决了! – wjang4

相关问题