Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of positive and negative numbers in a column

Tags:

r

count

I want to count the number of positive and negative values in one of the columns of the data frame. How would I do in R?

For example, here is the data frame for example

              logFC   logCPM       LR       PValue          FDR
Bra15066  -5.630822 5.184586 73.79927 8.647868e-18 4.060866e-13
Bra18809 -13.227825 7.158572 72.13478 2.009902e-17 4.719048e-13
Bra45310  5.848073 5.244367 65.61483 5.482472e-16 8.581530e-12
Bra44666  -4.270590 4.852193 63.75671 1.407731e-15 1.652605e-11
Bra34292 -12.917379 4.198073 61.84715 3.711794e-15 3.485968e-11
Bra38258  -5.239433 4.816886 57.98476 2.641567e-14 2.067378e-10

Now I would like to count the number of positive values in logFC column compared to negative values.

Basically I would like to see 5 counts for negative numbers and 1 for positive number for the above df. How would I do in R?

like image 709
upendra Avatar asked Sep 14 '13 00:09

upendra


3 Answers

Here's an even simpler solution:

table(sign(mydf$logFC))

The number of -1 and 1 results are your negative and positive counts, respectively.

like image 172
Scott Ritchie Avatar answered Sep 24 '22 13:09

Scott Ritchie


pos<-nrow(df[df$logFC>0,])
neg<-nrow(df[df$logFC<0,])

df is the dataframe, and pos and neg are the number of positive and negative entries in logFC

like image 25
John Paul Avatar answered Sep 21 '22 13:09

John Paul


Using count from plyr package:

library(plyr)
with(mydata,count(sign(logFC)))
like image 37
Metrics Avatar answered Sep 24 '22 13:09

Metrics