I have a dataframe as follows (called dat)
chr chrStart chrEnd Gene RChr RStart REnd Rname distance
chr1 39841 39883 Gene1 chr1 398 3984 Cha1b 0
chr1 39841 39883 Gene1 chr1 398 3985 Ab 0
chr1 39841 39883 Gene1 chr1 398 3986 Tia 0
chr1 39841 39883 Gene1 chr1 398 3987 MEA 0
chr1 39841 39883 Gene1 chr1 398 3988 La 0
chr1 39841 39883 Gene1 chr1 398 3989 M3 0
chr1 14893 15893 Gene2 chr1 398 3984 Cha1b 0
chr1 14893 15893 Gene2 chr1 398 3985 Cha1b 0
chr1 14893 15893 Gene2 chr1 398 3986 Cha1b 0
chr1 14893 15893 Gene2 chr1 398 3987 MEA 0
chr1 14893 15893 Gene2 chr1 398 3988 MEA 0
chr1 39841 39883 Gene1 chr1 398 3989 M3 0
I want to get the frequency that the different types of Rname appear for each gene so the result above should look like
Gene Rname Freq
Gene1 Cha1b 1
Gene1 Ab 1
Gene1 Tia 1
Gene1 MEA 1
Gene1 La 1
Gene1 M3 1
Gene2 Cha1b 3
Gene2 MEA 2
Gene2 M3 1
I tried doing two groupings with dplyr but I think it makes no sense and anyway it just gives me the frequency of all the Rnames for each gene
library(dplyr)
GroupTbb <- dat %>%
group_by(Gene) %>%
group_by(Rname) %>%
summarise(freq = sum(Rname))