2017-03-09 2 views
0

R에서 두 단어를 읽은 다음이 두 단어를 모두 포함하는 모든 문자열을 인쇄하는 함수를 작성하려고했습니다.R : 두 개의 문자열을 포함하는 문자열을 찾는 사용자 정의 함수

나는 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

관련 문제