2017-03-09 3 views
0

私は2つの単語を読み込んで両方の文字列を含むすべての文字列を出力する関数をRに書き込もうとしていました。R:2つの文字列を含む文字列を検索するユーザー定義関数

私はxという名前のdata.frameを持ち、最初の列は文字列を検索する場所です。

は私が書いた:

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に関する情報をお読みください:// [再現可能な例]を与える方法(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

関連する問題