upendra
upendra

Reputation: 2189

Count the number of positive and negative numbers in a column

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?

Upvotes: 9

Views: 24085

Answers (3)

John Paul
John Paul

Reputation: 12664

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

Upvotes: 3

Metrics
Metrics

Reputation: 15458

Using count from plyr package:

library(plyr)
with(mydata,count(sign(logFC)))

Upvotes: 2

Scott Ritchie
Scott Ritchie

Reputation: 10543

Here's an even simpler solution:

table(sign(mydf$logFC))

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

Upvotes: 24

Related Questions