1

I have a table like this example:

breeds <- "tag  Chr Position Breed
chr1-2157  1  2157 BRL
chr1-2157  1  2157 GOT
chr1-2157  1  2157 HED
chr1-2157  1  2157 KIN
chr2-2185  2  2185 BRL
chr2-2185  2  2185 GOT
chr2-2185  2  2185 HED
chr3-2189  3  2185 BRL"
breeds <-read.table(text=breeds,header=T)

And I would like to construct a logical table like this:

final <- "tag  BRL GOT HED KIN 
chr1-2157  TRUE  TRUE TRUE TRUE
chr2-2185  TRUE  TRUE TRUE FALSE
chr3-2189  TRUE  FALSE FALSE FALSE"
final <-read.table(text=final,header=T)

to finally classify the groups: The idea is to be able to count the number of unique tags of the possible "true" and "false" groups to finally have the number of each group to be included in a Venn Diagram using venneuler package (Venn diagram proportional and color shading with semi-transparency)

zx8754
  • 52,746
  • 12
  • 114
  • 209
Fpertille
  • 383
  • 1
  • 14

2 Answers2

3

You can calculate this with the table function along with a logical comparison:

with(breeds, table(tag, Breed)) > 0
           Breed
tag          BRL   GOT   HED   KIN
  chr1-2157 TRUE  TRUE  TRUE  TRUE
  chr2-2185 TRUE  TRUE  TRUE FALSE
  chr3-2189 TRUE FALSE FALSE FALSE

table by itself will produce frequency counts. Adding > 0 returned the logicals. with is just to reduce typing and maybe for a bit of clarity.

lmo
  • 37,904
  • 9
  • 56
  • 69
1

table is definitely easier. Another approach is to split breeds first by tag then by Breed and count the number of rows in each sub-group.

t(sapply(split(breeds, breeds$tag), function(a)
           sapply(split(a, a$Breed), function(b)
                                       nrow(b)>0 )))

#           BRL   GOT   HED   KIN
#chr1-2157 TRUE  TRUE  TRUE  TRUE
#chr2-2185 TRUE  TRUE  TRUE FALSE
#chr3-2189 TRUE FALSE FALSE FALSE 

Just like in lmo's solution, what you initially get is frequency counts and adding >0 returns logicals.

d.b
  • 32,245
  • 6
  • 36
  • 77