-2

I have a dataframe as follows:

row.names   V1  V2  V3  Rptname V5  V6  V7  V8  V9
1   14651   chr1    61025432    61029742    MIRb    chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
2   14652   chr1    61036393    61037479    L2a chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
3   14653   chr1    61039074    61041631    MIRb    chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
4   14654   chr1    61040277    61041060    L1PA13  chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
5   14655   chr1    61042625    61045428    L2a chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
6   14656   chr1    61048474    61050832    MLT1B   chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
7   14657   chr1    61053709    61057268    L2a chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
8   14658   chr1    61059515    61060022    L2a chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 0
9   14659   chr1    61065721    61066525    MLT1B   chr1    61025451    62627425    hsa-mir-3116-2_NFIA_INADL_TM2D1 

I would like to calculate the frequency of the Rptname column using dplyr.

I had used this code:

library(dplyr)

GroupedTableProportionampBRCA <- 
  ampBRCA_Filtered %>% 
  group_by(Rptname) %>% 
  summarise(freq = sum(Rptname))

but something has changed and now it tells me:

Error: ‘sum’ not meaningful for factors

Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • [R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate](http://stackoverflow.com/questions/3505701), [Aggregate multiple variables simultaneously](http://stackoverflow.com/questions/9723208), [How to sum a variable by group?](http://stackoverflow.com/q/1660124) – zx8754 Feb 22 '16 at 10:46
  • 2
    These are valid answers. But the question was specifically with reference to dplyr for lots of reasons that weren't necessary detail here but which the question is clear about. I notice dplyr has been removed from the title- presumably to justify voting it down? Change the question to justify the answer? That's a new one for me – Sebastian Zeki Feb 22 '16 at 10:58
  • 3
    I don't think this error is specific for dplyr, I removed it and added dplyr tag instead. `sum(as.factor(1:10))` – zx8754 Feb 22 '16 at 11:00

1 Answers1

2

We use n() for frequency not sum()

df %>% group_by(Rptname) %>% summarise(freq = n())
#  Rptname  freq
#   (fctr) (int)
#1  L1PA13     1
#2     L2a     4
#3    MIRb     2
#4   MLT1B     2

Or use count

df %>% count(Rptname)
mtoto
  • 23,919
  • 4
  • 58
  • 71