0

2 つのバイナリ ベクトルがあるとします。

male  female unknown
0     1      0
0     1      0
1     0      0
0     0      1
1     0      0

このデータのヒストグラムが必要です。同様に、このようなベクトルが他にもある可能性がある他のベクトルのヒストグラムが必要です。すべてのベクトルは同じ長さであり、同じデータ フレームからのものであると想定しても安全です。

各ベクトルをカウントまたは合計することでこれを行う方法を考えていますが、これを行うための良い方法または正しい方法は何ですか?

Tnx。

4

1 に答える 1

1

This is some strange data to be looking at presenting with a histogram, but never mind. I'd use ggplot2. If you melt the data (using reshape2) then the number of vectors or their length is irrelevant.

df <- data.frame(male=c(0,0,1,0,1),
                 female=c(1,1,0,0,0),
                 unknown=c(0,0,0,1,0))

df.m <- melt(df)
str(df.m)
hist(df.m$value ~ df.m$variable)
ggplot(df.m, aes(value)) + geom_histogram(aes(fill=variable)) +
  facet_wrap(~variable) #This depends on how you want your different variables split up.

ここに画像の説明を入力 #If you want them on the same plot, then:

ggplot(df.m, aes(value)) + geom_histogram(aes(fill=variable), position="dodge")
于 2013-02-28T12:05:19.543 に答える