问题描述
我有一个大的数据集列IDNUM,变量1,VAR2,VAR3,VAR4,VAR5,Var6。变量是与值0或1的布尔每一行可以是64个不同的可能的排列之一。我想算对应于每个排列present行数。有没有到R中写一个有效的方法?
I have a large dataset with columns IDNum, Var1, Var2, Var3, Var4, Var5, Var6. The variables are boolean with value either 0 or 1. Each row could be one of 64 different possible permutations. I would like to count the number of rows corresponding to each permutation present. Is there an efficient way to write this in R?
推荐答案
汇总
可以做到这一点。这里有一个更短的例子:
aggregate
can do this. Here's a shorter example:
r <- function() rbinom(10, 1, .5)
d <- data.frame(IDNum=1:10, Var1=r(), Var2=r())
d
IDNum Var1 Var2
1 1 0 1
2 2 0 1
3 3 0 0
4 4 1 0
5 5 1 1
6 6 0 0
7 7 1 1
8 8 1 0
9 9 0 1
10 10 0 1
现在来计算每个组合的号码:
Now to count the number of each combination:
> aggregate(d$IDNum, d[-1], FUN=length)
Var1 Var2 x
1 0 0 2
2 1 0 2
3 0 1 4
4 1 1 2
实际上并不用在这里 D $ IDNUM
的值,但是必须采取某种传递到长度
功能。中的数值 D $ IDNUM
每个组合传递到长度
得到计数。
The values in d$IDNum
aren't actually used here, but something must be passed to the length
function. The values in d$IDNum
for each combination are passed to length
to get the count.
这篇关于计数在研发现有的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!