1

allWordsは 130 万語のベクトルで、多少の繰り返しがあります。私がやりたいことは、2 つのベクトルを作成することです。

Aという言葉で

単語の出現を伴うB

後でそれらをマトリックスに結合して、次のように関連付けることができるように: "mom", 3 ; 「鉛筆」、14など

for(word in allWords){

    #get a vector with indexes for all repetitions of a word
    temp <- which(allWords==word) 
    #Make "allWords" smaller - remove duplicates
    allWords= allWords[-which(allWords==word)]
    #Calculate occurance
    occ<-length(temp)
    #store
    A = c(A,word)
    B = c(B,occ)
}

この for ループには永遠に時間がかかり、なぜ、または何が間違っているのか本当にわかりません。ファイルから 130 万ワードを読み取るのは 5 秒ほどの速さですが、これらの基本的な操作を実行しても、アルゴリズムが終了することはありません。

4

3 に答える 3

3

使用table():

> table(c("dog", "cat", "dog"))

cat dog 
  1   2 

ベクトルは、対応するデータフレームの列です。

A <- as.data.frame(table(c("dog", "cat", "dog")))[,1]
B <- as.data.frame(table(c("dog", "cat", "dog")))[,2]

結果:

> A
[1] cat dog
Levels: cat dog
> B
[1] 1 2
于 2013-09-28T21:29:40.757 に答える
2

data.tableあなたのベクトルのサイズを教えてください。この状況では良い友達になると思います_

> library(data.table)
> x <- c("dog", "cat", "dog")  # Ferdinand.kraft's example vector
> dtx <- data.table(x)         # converting `x` vector into a data.table object
> dtx[, .N, by="x"]            # Computing the freq for each word
     x N
1: dog 2
2: cat 1
于 2013-09-28T21:46:45.553 に答える
0

listハッシュの「キー:値」ペアのようなものを作成するために使用できます。

data = c("joe", "tom", "sue", "joe", "jen")

aList = list()

for(i in data){
    if (length(aList[[i]]) == 0){
      aList[[i]] = 1
    } else {
      aList[[i]] = aList[[i]] + 1
    }
}   

結果

$joe
[1] 2

$tom
[1] 1

$sue
[1] 1

$jen
[1] 1
于 2013-09-28T22:11:33.793 に答える