2

特定の基準を満たすデータ フレーム内のデータポイントの数を確認するにはどうすればよいですか?

Group<-c("Group 1","Group 1","Group 1","Group 2","Group 2")
Factor<-c("Factor 1", "Factor 1", "Factor 2", "Factor 1", "Factor 2")

data<-data.frame(cbind(Group,Factor))
data

たとえば、グループ 1、因子 2 に含まれるデータ ポイントの数を知りたいと思います。summary関数を使用できるはずだと思いますが、因子レベルの組み合わせが必要であることを指定する方法がわかりません。例えば:

summary(data) #Verifies that Group 1 appears 3 times, and Factor 2 appears twice, but no information on how many times Group 1 AND Factor 2 appear in the same row

では、さまざまな因子水準の組み合わせの要約表を取得するにはどうすればよいでしょうか。

4

1 に答える 1

4

論理テストを使用してsumから、特定の組み合わせを使用します。

sum(with(data,Group=="Group 1" & Factor=="Factor 2"))
[1] 1

これを拡張するには、次のように簡単に使用できますtable

with(data,table(Group,Factor))

         Factor
Group     Factor 1 Factor 2
  Group 1        2        1
  Group 2        1        1

...これを に戻すと、data.frameちょっとした要約データセットが得られます。

data.frame(with(data,table(Group,Factor)))

    Group   Factor Freq
1 Group 1 Factor 1    2
2 Group 2 Factor 1    1
3 Group 1 Factor 2    1
4 Group 2 Factor 2    1
于 2013-08-09T04:53:14.297 に答える