2017-11-21 4 views
1

다른 문자 벡터를 사용하여 문자 벡터를 스캔하는 방법을 찾고 있습니다. 나는 이미 이렇게 많은 시간을 쏟아 부었지만, 제대로 할 수없는 것처럼 보였다. 내가하려는 일을하는 기능을 찾을 수 없습니다. 하지만이다른 문자 벡터를 사용하여 특정 문자 벡터 값 찾기 R

가 그래서 나는 다음과 같은 벡터가 있다고 가정 해 봅시다 해결하는 쉬운 방법이있을거야 : 한편

c <- c("bread", "milk", "oven", "salt") 

나는 문장을 포함하는 벡터를 가지고있다.

text <- c("The BREAD is in the oven. Wonderful!!", 
    "We don't only need Milk to bake a yummy bread, but a pinch of salt as 
    well.", "Oven, oven, oven, why not just eat it raw.") 

이제 내 c 벡터의 내용을 사용하여 텍스트 블록을 스캔하고 싶습니다. 내가하고 싶은

           text bread milk oven salt 
    1  The BREAD is in the oven. Wonderful!! 1 0 1 0 
    2  We don't only need Milk... as well." 0 1 0 1 
    3 Oven, oven, oven, why not just eat it raw.  0 0 3 0 

또 다른 일이 아니라 단지 하나의 단어에 대한보다 조합을 검색 할 수 있습니다 : 출력은 그런 일을 보일 것입니다.

c <- c("need milk", "oven oven", "eat it") 

동일한 출력을 얻기 :

           text need milk oven oven eat it 
    1  The BREAD is in the oven. Wonderful!!  0   0  0 
    2  We don't only need Milk... as well."  1   0  1 
    3 Oven, oven, oven, why not just eat it raw.  0   2  1 

누군가가 나를 도울 수 있다면 그것은 좋은 것입니다! :) 정말 고맙습니다!

+2

'data.frame (text, + (sapply (c, grepl, tolower (text)))))' – akrun

+0

을 사용해보세요. 이해를 도울 수 있을까요? 여기서'+'무엇을합니까? – amrrs

+3

@amrrs'grepl'은 논리 행렬을 반환합니다. '+'또는'*'1을 추가함으로써, 바이너리로 변환합니다. – akrun

답변

4

우리는 다른 해결책 stringi 패키지를 사용하여 '문자열'여기

library(stringr) 
data.frame(text, sapply(c, str_count, string = tolower(text))) 
1

pattern의 발생 수를 계산하는 str_count을 사용할 수있는 최소 속도면 (단순 직결되지 않음) 비트로 다른 접근 방식. 물론 단순성과 속도를 고려하여 기본 R을 사용하는 경우에는 "비트"가 의미하는 바에 달려 있습니다.

또 다른 언급은 grepl 솔루션은 위의 설명에 표시된대로 실제 카운트가 아닌 2 진수를 반환한다는 것입니다 . 그래서 그것의 직접적인 비교가되지 않습니다. 그러나 필요에 따라 충분할 수 있습니다.

library(stringi) 
library(stringr) 
library(microbenchmark) 

c <- c("bread", "milk", "oven", "salt") 
text <- c("The BREAD is in the oven. Wonderful!!", 
      "We don't only need Milk to bake a yummy bread, but a pinch of salt as 
      well.", "Oven, oven, oven, why not just eat it raw.") 


stringi_approach <- function() { 

    matches <- sapply(c, function(w) {stri_count_fixed(text,w, case_insensitive = TRUE)}) 
    rownames(matches) <- text 

} 

grepl_approach <- function() { 

    df <- data.frame(text, +(sapply(c, grepl, tolower(text)))) 

} 

stringr_approach <- function() { 

    df <- data.frame(text, sapply(c, str_count, string = tolower(text))) 

} 

microbenchmark(
    grepl_approach(), 
    stringr_approach(), 
    stringi_approach() 
) 

# Unit: microseconds 
#   expr  min  lq  mean median  uq  max neval 
# grepl_approach() 309.091 338.500 351.3017 347.5790 352.7105 565.679 100 
# stringr_approach() 380.541 418.634 437.7599 429.2925 441.7275 814.767 100 
# stringi_approach() 101.057 113.492 126.9763 129.4790 133.8215 217.903 100 
0

당신은이에 대한 코퍼스 라이브러리를 사용할 수 있습니다 :

library(corpus) 
library(Matrix) 

text <- c("The BREAD is in the oven. Wonderful!!", 
    "We don't only need Milk to bake a yummy bread, but a pinch of salt as 
    well.", "Oven, oven, oven, why not just eat it raw.") 

term_matrix(text, select = c("bread", "milk", "oven", "salt")) 
## 3 x 4 sparse Matrix of class "dgCMatrix" 
##  bread milk oven salt 
## [1,]  1 . 1 . 
## [2,]  1 1 . 1 
## [3,]  . . 3 . 

term_matrix(text, select = c("need milk", "oven oven", "eat it"), drop_punct = TRUE) 
## 3 x 3 sparse Matrix of class "dgCMatrix" 
##  need milk oven oven eat it 
## [1,]   .   .  . 
## [2,]   1   .  . 
## [3,]   .   2  1 

다른 방법으로, 마누엘 BICKEL의 답변 중 하나를 수정할 수 text_count 대신 str_count의를 사용하여.