2016-09-12 2 views
9

私は、顧客ID(下記参照)などのkwic出力にメタデータを追加して、マスターファイルを参照しやすくしたいと考えています。私はcbindを使用してデータを追加しようとしましたが、何も正しく一致しません。クォンタカタカナは出力にデータを追加します

可能であれば、このような例があれば幸いです。

 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はコーパスで作業しているため、元のデータフレームの他の列を認識していない可能性があり、コーパスはデータフレームではなく他の種類のデータから読み込むように設計されています。 'docvar'に列を追加することができます。この列は、著者が望む場合にはKWICがアクセスできます。パッケージ作成者は、コーパスのプロットやレポートにのみ使用しているように見えましたが。 – dracodoc

+1

@Pierre 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

関連する問題