如果在R多种条件下

时间:2016-12-05 18:24:44

标签: r

我有以下问题但不知道。

我的(负责人)数据如下所示:

tag.1 tag.2 tag.3 tag.4 tag.5 tag.6 tag.7 tag.8 tag.9 tag.10 Sex

它只包含1和0.

我的动画是创建一个新表。如果在一天的列中有1,我想总结m(= 0)和f(= 1)。所以我每天都会有一定数量的女性和男性。

如果你可以帮助我,那会很好。

数据?我不知道如何在这里插入我的矩阵 输出?新表,每个采样日的男性/女性总和

    tag 1 tag 2 tag 3 tag 4 tag 5 tag 6 tag 7 tag 8 tag 9 tag 10 Sex
1       1     0     0     0     0     0     0     0     0      0   0
2       1     0     0     0     0     0     0     0     0      0   0
3       1     0     0     0     0     0     0     0     0      0   0
4       1     0     0     0     0     0     1     0     0      0   1
5       1     0     0     0     0     0     0     0     0      0   1
6       1     0     0     0     0     0     0     0     0      0   1
7       1     0     0     0     0     0     0     0     0      0   1
8       1     1     0     0     0     0     0     0     0      0   0
9       1     0     0     0     0     0     0     0     0      0   0
10      1     0     0     0     0     0     0     0     0      0   0
11      1     0     0     0     0     0     0     0     0      0   1
12      1     1     0     0     0     0     0     0     0      0   0
13      1     0     0     0     0     0     0     0     0      0   0
14      1     1     0     0     0     0     0     0     0      0   0
15      1     0     0     0     0     0     0     0     0      0   0
16      1     0     0     0     0     0     0     0     0      0   0
17      1     0     0     1     0     0     0     0     0      0   0
18      1     0     0     0     0     0     0     0     0      0   0
19      1     0     1     0     0     0     0     0     0      0   0
20      1     1     0     0     1     0     1     1     0      0   0
21      1     0     0     0     0     0     0     0     0      0   0
22      1     1     0     0     0     0     0     0     0      0   1
23      1     0     0     0     0     0     0     0     0      0   0
24      0     1     0     0     0     0     0     0     0      0   1
25      0     1     0     0     0     0     0     0     0      0   1
26      0     1     0     0     0     0     0     0     0      0   0
27      0     1     0     0     1     0     0     0     0      0   0
28      0     1     0     0     0     1     0     0     0      0   0
29      0     1     0     0     0     0     0     0     0      0   0
30      0     1     0     0     0     0     0     0     0      0   0
31      0     1     0     1     0     0     0     0     0      0   0
32      0     1     0     0     0     0     0     0     0      0   0
33      0     1     0     0     0     0     0     0     0      0   0
34      0     1     0     0     0     0     0     0     0      0   0
35      0     1     0     0     0     0     0     0     0      0   1
36      0     1     0     0     0     0     0     0     0      0   0
37      0     1     0     0     0     0     0     0     0      0   0
38      0     1     1     0     0     0     0     0     0      0   1
39      0     1     1     1     1     1     0     0     0      0   0
40      0     1     0     0     0     0     0     0     0      0   0
41      0     1     0     0     0     0     0     0     0      0   1
42      0     1     0     0     0     0     0     0     0      1   0
43      0     1     0     0     0     0     0     0     0      0   0
44      0     1     0     0     0     0     0     0     0      0   1
45      0     1     0     0     0     0     0     0     0      0   0
46      0     1     0     0     0     0     0     0     0      0   0
47      0     1     0     0     0     0     0     0     0      0   1
48      0     1     1     1     1     0     0     0     0      0   1
49      0     0     1     1     1     0     0     0     0      0   0
50      0     0     1     0     0     0     0     0     0      0   0
51      0     0     1     1     0     0     0     0     0      0   0
52      0     0     1     0     0     0     0     0     0      0   1
53      0     0     1     1     1     0     1     0     0      0   0
54      0     0     1     0     1     0     1     0     0      0   1
55      0     0     1     0     0     1     0     1     0      0   0
56      0     0     1     0     0     0     0     0     0      0   1
57      0     0     1     0     0     0     0     1     0      0   1
58      0     0     1     0     0     0     0     0     0      0   0
59      0     0     1     0     0     0     0     0     0      0   0
60      0     0     1     0     1     0     0     0     0      0   1
61      0     0     0     1     0     0     0     0     0      0   0
62      0     0     0     1     0     0     0     0     0      0   0
63      0     0     0     1     0     0     0     0     0      0   1
64      0     0     0     1     0     1     0     0     0      0   0
65      0     0     0     1     0     0     0     0     0      0   1
66      0     0     0     1     0     0     0     0     0      0   0
67      0     0     0     1     0     0     0     0     0      0   1
68      0     0     0     1     0     0     1     0     0      0   1
69      0     0     0     1     0     0     0     0     0      0   0
70      0     0     0     1     0     0     0     0     0      0   1
71      0     0     0     1     0     0     0     0     0      0   1
72      0     0     0     1     0     0     0     0     0      0   0
73      0     0     0     0     1     1     1     0     0      0   1
74      0     0     0     0     1     0     0     0     1      0   1
75      0     0     0     0     1     0     0     0     0      0   1
76      0     0     0     0     1     1     0     0     0      0   1
77      0     0     0     0     1     0     0     0     0      0   1
78      0     0     0     0     1     0     0     0     0      0   0
79      0     0     0     0     1     1     1     1     0      0   0
80      0     0     0     0     1     0     0     0     0      0   0
81      0     0     0     0     1     0     0     0     0      0   0
82      0     0     0     0     1     0     0     0     0      0   0
83      0     0     0     0     1     1     0     0     0      0   0
84      0     0     0     0     1     0     0     0     0      0   0
85      0     0     0     0     1     0     0     0     1      1   1
86      0     0     0     0     1     1     1     0     0      0   1
87      0     0     0     0     1     0     0     0     0      0   0
88      0     0     0     0     1     0     1     0     0      0   0
89      0     0     0     0     1     0     1     0     0      0   1
90      0     0     0     0     1     0     0     0     0      0   0

1 个答案:

答案 0 :(得分:1)

也许这会对你有帮助。

我的虚拟数据:

df <- data.frame(tag1 = sample(0:1, 10, replace = T),
                 tag2 = sample(0:1, 10, replace = T),
                 tag3 = sample(0:1, 10, replace = T),
                 tag4 = sample(0:1, 10, replace = T),
                 sex = sample(0:1, 10, replace = T))

首先,让你的矩阵成为一个data.frame(如果它还没有):

df <- as.data.frame(your_matrix)

然后,使用tidyr::gatherdplyr::group_bydplyr::tally

library(dplyr); library(tidyr)

#make it "long" format
df2 <- gather(df, key = "tag", value = "value", -sex)

# tally by the grouping variables
df2 %>% group_by(sex, tag) %>% tally()

Source: local data frame [8 x 3]
Groups: sex [?]

    sex   tag     n
  <int> <chr> <int>
1     0  tag1     6
2     0  tag2     6
3     0  tag3     6
4     0  tag4     6
5     1  tag1     4
6     1  tag2     4
7     1  tag3     4
8     1  tag4     4