conditional count of occurrence in R
1
0
Entering edit mode
24 months ago
mthm ▴ 50

I know how to count repeated fields in a column simply by using table(file$column)

> exon    intron    CDS    mRNA
   234    235      7564    27465

now I want to know how I can count these fields in a column based on the values of another column, e.g.

col1      col2     col3    col4   col5  col6  col7   col8   col9
scf001    74212    85524    mRNA    -    ERV    1659   1689   30
scf002    35002    48200    CDS     +    ERV2    16     81    62
scf0101    82177    82314   exon    -    DUF    465    615    137
scf00273   74212    85524   intron  +    AZUR    612   766    135

I want to count the number of occurrence for col4 for values of equal or greater than 80 in col9, how can I do that?

R occurrence table • 795 views
ADD COMMENT
1
Entering edit mode
> table(df$col4[df$col9>=80])

  exon intron 
     1      1 

> table(df$col4[df$col9>=80]) |>
+   as.data.frame()

    Var1 Freq
1   exon    1
2 intron    1
ADD REPLY
2
Entering edit mode
24 months ago
acvill ▴ 340

In the tidyverse:

data %>% filter(col9 >= 80) %>% select(col4) %>% table
ADD COMMENT
0
Entering edit mode

Thanks, can you explain what %>% does in this package?

ADD REPLY
1
Entering edit mode

%>% is the dplyr pipe: https://style.tidyverse.org/pipes.html. It passes the output of one command to the input of the next.

ADD REPLY

Login before adding your answer.

Traffic: 2575 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6