1

2 つの質問 (列Q1_bQ2_b) を比較して、それらを (同じバープロット内で) 並べてバープロットしようとしています。回答のオプションは 1 ~ 6 です。問題は、 に対して4 と答えた人が誰もいなかったことです。そのため、バープロットは、 に対して 4 と答えた人のパーセンテージの横に、Q1_bに対して 4 があるはずの場所に 5 を表示するまでスキップします。特定のオプションに対する回答がなかった場合、R がこれを行わず、自動的に 0% 列を入力するようにするにはどうすればよいですか?Q1_bQ2_b

alldataset<-structure(list(Q1_b = c(6L, 1L, 5L, 3L, 5L, 6L, 6L, 2L), 
                           Q2_b = c(1L, 2L, 2L, 5L, 4L, 3L, 6L, 1L)), 
                      .Names = c("Q1_b", "Q2_b"), 
                      class = "data.frame", 
                      row.names = c(NA, -8L))

Qb<-table(alldataset$Q2_b)
Qf<-table(alldataset$Q1_b)

nrowFUP<-NROW(alldataset$Q1_b)
nrowBL<-NROW(alldataset$Q2_b)

options(digits=6)
newbl <- transform(as.data.frame(table(alldataset$Q2_b)),    
                 percentage_column=Freq/nrowBL*100)

newfup <- transform(as.data.frame(table(alldataset$Q1_b)), 
                    percentage_column=Freq/nrowFUP*100)


matrixQ1<-cbind(newbl$percentage_column, newfup$percentage_column)

matrixQ1dataframe<-data.frame(matrixQ1)
rmatrixQ1<-as.vector(t(matrixQ1dataframe))
roundedrmatrix<-round(rmatrixQ1, digits=0)
barplotmatrix<-matrix(roundedrmatrix)

par(mar=c(7.5,4,3,2), mgp=c(2,.7,0), tck=-.01, las=1, xpd=TRUE)

 b<-barplot(matrix(roundedrmatrix, nr=2), 
           beside=T, xlab="",
           ylab="Percentage",
           cex.lab=0.9, 
           main="Comparison",
           cex.main=0.9, ylim=c(0,70), 
           col=c("black","yellow"), 
           names.arg=c(1:6),
           legend=c("Q2_b","Q1_b"),
           args.legend=list(x="bottomleft", 
                            cex=0.8,
                            inset=c(0.4,-0.4)))
text(x=b, y=roundedrmatrix,labels=roundedrmatrix, pos=3, cex=0.8)

R は、次のように表示して、これが発生することも警告します。

Warning message:
In cbind(newbl$percentage_column, newfup$percentage_column) :
  number of rows of result is not a multiple of vector length (arg 2)

私はこれを整理するために何年も努力してきましたが、どこにも行きません。誰でも助けることができますか?

4

2 に答える 2

3

問題は、ベクトルが1〜6の潜在的な値を持つカテゴリ応答を表すことをRに伝えたことがないため、0カウントを含めることを知らないことです(7、8、100万の場合は0を含めたくないでしょう。等。)。

最初の2行を次のように置き換えてみてください。

Qb<-table(factor(alldataset$Q2_b, levels=1:6))
Qf<-table(factor(alldataset$Q1_b, levels=1:6))

または次のようなものを実行します:

alldataset$Q1_b <- factor(alldataset$Q1_b, levels=1:6)
alldataset$Q2_b <- factor(alldataset$Q2_b, levels=1:6)

テーブルコマンドの前。

于 2012-11-22T14:31:47.517 に答える
2

tableで 1 から 6 までのすべての値を使用するように指示する必要がありますtable(factor(x, seq.int(6)))

コードの改良版は次のとおりです。

dat <- t(round(sapply(rev(alldataset),
                      function(x) table(factor(x, seq.int(6)))) / 
                                                         nrow(alldataset) * 100))

par(mar=c(7.5,4,3,2), mgp=c(2,.7,0), tck=-.01, las=1, xpd=TRUE)
b <- barplot(dat, beside=T,xlab="", ylab="Percentage", cex.lab=0.9, 
             main="Comparison", cex.main=0.9, ylim=c(0,70), 
             col=c("black","yellow"), names.arg=c(1:6), legend=names(dat), 
             args.legend=list(x="bottomleft", cex=0.8, inset=c(0.4,-0.4)))
text(x=b, y=dat,labels=dat, pos=3, cex=0.8)

ここに画像の説明を入力

于 2012-11-22T14:34:15.020 に答える