2016-09-12 2 views
9

고객 ID (아래 참조)와 같은 kwic 출력에 메타 데이터를 추가하여 마스터 파일을 쉽게 조회 할 수 있습니다. 나는 cbind를 사용하여 데이터를 추가하려고 시도했지만 아무 것도 올바르게 일치하지 않습니다.Quantityakwic 출력에 데이터를 추가하십시오.

가능한 경우 예를 들어 주시면 감사하겠습니다.

 docname position contextPre  keyword contextPost   CustID 
    text3790  5 nothing at all looks good and sounds great   1 
    text3801 11 think the offer is a good value and has a lot  3 
    text3874 10 not so sure thats a good word to use    5 

발생 data.frame

 CustID Comment 
     1  nothing at all looks good and sounds great 
     2  did not see anything that was very appealing 
     3  I think the offer is a good value and has a lot of potential 
     4  these items look terrible how are you still in business 
     5  not so sure thats a good word to use 
     6  having a hard time believing some place would sell an item so low 
     7  it may be worth investing in some additional equipment 

답변

5

을 나는 이상적인 솔루션이 docvars을 사용하는 것입니다 생각 , 그러나 kwic은 그들을 보여줄 수있는 선택권이없는 것 같습니다. 나는 여전히 id-doc 매핑 테이블을 kwic 결과와 병합해야한다.

library(data.table) 
library(quanteda) 

s <- "CustID, Comment 
1,  nothing at all looks good and sounds great 
2,  did not see anything that was very appealing 
3,  I think the offer is a good value and has a lot of potential 
4,  these items look terrible how are you still in business 
5,  not so sure thats a good word to use 
6,  having a hard time believing some place would sell an item so low 
7,  it may be worth investing in some additional equipment" 

# I'm using data.table mainly to read the data easily. 
dt <- fread(s, data.table=FALSE) 

# all operations below apply to data frame 
myCorpus <- corpus(df$Comment) 
# the Corpus and CustID came from same data frame, 
# thus ensured the mapping is correct 
docvars(myCorpus, "CustID") <- df$CustID 
summary(myCorpus) 
# build the mapping table of docname and CustID. 
# The docname is in row.names, have to make an explicit column 
dv_table <- docvars(myCorpus) 
id_table <- data.frame(docname = row.names(dv_table), CustID = dv_table$CustID) 
result <- kwic(myCorpus, "good", window = 3, valuetype = "glob") 
id_result <- merge(result, id_table, by = "docname") 

결과 : 피드백

> id_result 
    docname position contextPre keyword  contextPost CustID 
1 text1  5 at all looks good and sounds great  1 
2 text3  7 offer is a good value and has   3 
3 text5  6 sure thats a good word to use   5 
+0

감사합니다. 나는 병합이나 조인의 어떤 유형이 요구 될 것이라는 것을 깨닫고 천천히 가고 있었다. 출력의 일부로 원본 데이터 프레임의 다른 열을 포함시킬 수도 있다면 KWIC의 훌륭한 기능입니다. 건배. – Atwp67

+0

KWIC는 원본 데이터 프레임의 다른 열을 인식하지 못합니다. 이는 해당 데이터가 corpus에서 작동하고 있고 corpus가 데이터 프레임 대신 다른 유형의 데이터에서 읽도록 설계 되었기 때문입니다. 'docvar'에 열을 추가 할 수 있습니다.이 열은 저자가 원하는 경우 KWIC에서 액세스 할 수 있습니다. 꾸러미 저자는 코퍼스의 줄거리 나 보고서에 단지 그들을 사용하는 것 같았지 만. – dracodoc

+1

감사합니다 @ 피에르 Lafortune, 전에'data.table = FALSE' 옵션을 몰랐어요! – dracodoc

1

당신이 열을 일반적인 방법으로 추가 할 수 있도록 그것은 data.frame 객체입니다 처음에는

library(quanteda) 
h <- head(kwic(inaugTexts, "secure*", window = 3, valuetype = "glob")) 

#Add new ID column 
h$CustID <- 1:nrow(h) 
+0

감사합니다. 새 열을 추가하면 작동하지만 궁극적으로 올바른 고객 ID를 명세서에 할당 한 것입니다. – Atwp67

+0

이렇게 할 수도 있습니다. 질문에서 ID 매핑을 제공하지 않은 이유는 무엇입니까? –

+0

그게 내가 퍼즐의 조각을 함께 넣어 묻는거야. – Atwp67

관련 문제