3

Rtable()関数を使用して作成された 2 つの頻度テーブルがあります。

freq1 <- table(unlist(strsplit(topic_list1, split=";")))
freq2 <- table(unlist(strsplit(topic_list2, split=";")))

topic_list1topic_list2は、 で区切られたトピックのテキスト表現を含む文字列です;

可能であれば、2 つの周波数をグラフで比較する方法が必要です。

したがって、2 つのリストに頻度の異なる同じトピックが含まれている場合は、それを表示できるようにしたいと考えています。1 つの度数分布表に存在するトピックについても同じことが言えますが、もう 1 つの頻度表には存在しません。

4

2 に答える 2

2

これを行うにはおそらくもっとエレガントな方法がありますが、これはうまくいくはずです:

# here I'm generating some example data
set.seed(5)
topic_list1 <- paste(sample(letters, 20, replace=T), sep=";")
topic_list2 <- paste(sample(letters, 15, replace=T), sep=";")

# I don't make the tables right away
tl1      <- unlist(strsplit(topic_list1, split=";"))
tl2      <- unlist(strsplit(topic_list2, split=";"))
big_list <- unique(c(tl1, tl2))

# this computes your frequencies
lbl         <- length(big_list)
tMat1       <- matrix(rep(tl1, lbl), byrow=T, nrow=lbl)
tMat2       <- matrix(rep(tl2, lbl), byrow=T, nrow=lbl)
tMat1       <- cbind(big_list, tMat1)
tMat2       <- cbind(big_list, tMat2)
counts1     <- apply(tMat1, 1, function(x){sum(x[1]==x[2:length(x)])})
counts2     <- apply(tMat2, 1, function(x){sum(x[1]==x[2:length(x)])})
total_freqs <- rbind(counts1, counts2, counts1-counts2)

# this makes it nice looking & user friendly
colnames(total_freqs) <- big_list
rownames(total_freqs) <- c("topics1", "topics2", "difference")
total_freqs           <- total_freqs[ ,order(total_freqs[3,])]
total_freqs
            d  l  a  z  b f s y m r x h n i g k c v o
topics1     0  0  0  0  0 2 1 1 1 1 2 2 1 1 1 1 2 2 2
topics2     2  2  2  1  1 2 1 1 1 0 1 1 0 0 0 0 0 0 0
difference -2 -2 -2 -1 -1 0 0 0 0 1 1 1 1 1 1 1 2 2 2

そこから、単純な数値を使用するか、必要に応じて視覚化することができます (例: ドットプロットなど)。簡単なドットプロットを次に示します。

windows()
  dotchart(t(total_freqs)[,3], main="Frequencies of topics1 - topics2")
  abline(v=0)

ここに画像の説明を入力

于 2013-09-03T15:13:50.197 に答える