0

I was trying to write a function in R that reads two words from me and then prints out all strings that contains both of those two words.

I have a data.frame named x and first column is where I need to search the strings from.

I wrote:

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,])}

This isn't working... What did I miss? Please help.

wjang4
  • 127
  • 1
  • 13
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Mar 09 '17 at 20:44

1 Answers1

0

Depending on what you exactly want, you might need to adapt this a bit, but this might help you:

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)
user3640617
  • 1,546
  • 13
  • 21