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

...如果你转换回 adata.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 回答