0

i have got the following problem and no idea.

My (head of) data looks like this:

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

it contains only 1 and 0.

My aime is to create a new table. I want to sum up for both m(=0) and f(=1), if there is an 1 in the column of day. So I would have an concrete number of females and males for each day.

Would be nice if you could help me with that.

data? i dont know how to insert my matrix here output? new table with the sum of males/females per each samplingday

    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
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Frieba
  • 17
  • 2

1 Answers1

1

maybe this will help you.

my dummy data:

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))

first, make your matrix a data.frame (if it's not already):

df <- as.data.frame(your_matrix)

then, use tidyr::gather, dplyr::group_by and dplyr::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
Nate
  • 10,361
  • 3
  • 33
  • 40
  • Thanks a lot! But i think there is a problem with the version. It sais i need R 3.1.3 and i am using the newest version. Could that be? – Frieba Dec 05 '16 at 19:16
  • maybe try reinstalling those two packages, I am running R 3.3.1 with no problems – Nate Dec 05 '16 at 19:18
  • holler if you are still having problems with it and we can figure it out – Nate Dec 05 '16 at 20:37
  • i installed the older version and it worked running the packages. but the output isnt what i expected. should be different sums of males/females for each day. oh man, i think i give up and use excel for this task. But thanks a lot thinking about it =) – Frieba Dec 05 '16 at 20:47
  • ahh no not excel ;) just curious what was your expected output, I'd hate to see you leave SO:R and go back to excel – Nate Dec 05 '16 at 22:04