I am trying to create a function that will make R able to read each singular unit (ID) and count the number of co-occurrences of specific characters in that unit. The dataset is here below:
ID class weight
1 1 A 1.0
2 1 A 1.0
3 1 B 1.0
4 2 A 1.0
5 2 B 1.0
6 2 C 1.0
7 3 B 1.0
8 4 B 1.0
9 4 C 1.0
10 4 C 1.0
11 4 D 1.0
12 4 D 1.0
13 5 A 0.9
14 5 B 0.9
15 5 C 0.9
16 5 D 0.9
17 6 B 0.8
18 6 B 0.8
19 7 C 0.7
20 7 C 0.7
21 7 D 0.7
22 7 D 0.7
23 8 C 0.6
24 8 D 0.6
25 9 D 0.5
26 9 E 0.5
27 9 E 0.5
28 10 C 0.4
29 10 C 0.4
30 10 C 0.4
31 10 E 0.4
32 11 A 0.3
33 11 A 0.3
34 11 A 0.3
35 12 A 0.2
36 12 B 0.2
37 12 C 0.2
38 13 B 0.1
39 13 D 0.1
40 13 D 0.1
41 13 E 0.1
42 14 D 1.0
43 14 E 1.0
44 15 B 1.0
45 15 B 1.0
46 15 C 1.0
47 15 C 1.0
48 15 D 1.0
49 16 C 1.0
50 16 D 1.0
51 16 E 1.0
52 16 E 1.0
53 17 B 1.0
54 17 C 1.0
55 17 C 1.0
56 18 D 1.0
57 18 D 1.0
58 18 E 1.0
59 19 E 1.0
60 19 E 1.0
61 20 B 1.0
62 20 D 1.0
63 20 E 1.0
64 20 E 1.0
I tried to create a loop function, but I don't know how to correctly specificy the expression. R should recognize ID from 1 up to 20, and in each ID count how many times the characters co-occur together. Not only that, each co-occurrence has to be weighted by the specific weight of the ID. Any thoughts about generating a loop function?
Some specifics: In ID 1 class A and B co-occur two times (first A with B and second A with B), which multiplied by the weight (1) gives a preliminary value of 2. The co-occurrence value of A and B should be 4.1 after the entire list is completed by the loop, and that value should be reported in a matrix 5x5 that looks like this:
A B C D E
A 1 4.1 ..
B 4.1 1 ..
C .. .. 1
D .. 1
E .. 1
Co-occurrence between identical classes will be just 1.
dput(data) structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L, 20L, 20L), class = c("A", "A", "B", "A", "B", "C", "B", "B", "C", "C", "D", "D", "A", "B", "C", "D", "B", "B", "C", "C", "D", "D", "C", "D", "D", "E", "E", "C", "C", "C", "E", "A", "A", "A", "A", "B", "C", "B", "D", "D", "E", "D", "E", "B", "B", "C", "C", "D", "C", "D", "E", "E", "B", "C", "C", "D", "D", "E", "E", "E", "B", "D", "E", "E"), weight = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9, 0.9, 0.9, 0.9, 0.8, 0.8, 0.7, 0.7, 0.7, 0.7, 0.6, 0.6, 0.5, 0.5, 0.5, 0.4, 0.4, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -64L), class = c("data.table", "data.frame"), .internal.selfref = ) gc() used (Mb) gc trigger (Mb) max used (Mb) Ncells 2672851 142.8 4316924 230.6 4316924 230.6 Vcells 5761794 44.0 12425324 94.8 29629603 226.1 library(data.table) data <- fread("toy.csv") dput(data) structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 20L, 20L, 20L, 20L), class = c("A", "A", "B", "A", "B", "C", "B", "B", "C", "C", "D", "D", "A", "B", "C", "D", "B", "B", "C", "C", "D", "D", "C", "D", "D", "E", "E", "C", "C", "C", "E", "A", "A", "A", "A", "B", "C", "B", "D", "D", "E", "D", "E", "B", "B", "C", "C", "D", "C", "D", "E", "E", "B", "C", "C", "D", "D", "E", "E", "E", "B", "D", "E", "E"), weight = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9, 0.9, 0.9, 0.9, 0.8, 0.8, 0.7, 0.7, 0.7, 0.7, 0.6, 0.6, 0.5, 0.5, 0.5, 0.4, 0.4, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -64L), class = c("data.table", "data.frame"), .internal.selfref = )
dput(mat)
does not match your first data frame. Was this intentional? Can youdput
the one at the top of your post? – Marian Minardput
has some issues, but selecting the trailingstructure
seems to replicate your data – Marian Minarid = n
, we havec("A", "A", "B", "B")
. Is this counted as 4 or 2, or ...? – Marian Minar