1
col1 col2
  -1    F
   2    T
  -1    F
  -2    F
   3    T

when F is selected in col2 I want to find out how many corresponding -ve and +ve values are recorded in col1.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Reju V S
  • 17
  • 1
  • 5
  • Duplicate of [Subset / filter rows in a data frame based on a condition in a column](https://stackoverflow.com/questions/3445590/subset-filter-rows-in-a-data-frame-based-on-a-condition-in-a-column) (how to select relevant rows); [Count the number of positive and negative numbers in a column](https://stackoverflow.com/questions/18797247/count-the-number-of-positive-and-negative-numbers-in-a-column) – Henrik Mar 07 '21 at 23:34

1 Answers1

0

We get the subset of 'col1' values where the 'col2' is "F", get the sign, count those with table in base R

with(df1, table(sign(col1[col2 == 'F'])))

Or using dplyr

library(dplyr)
df1 %>%
   filter(col2 == 'F') %>%
   mutate(col1 = sign(col1)) %>%
   count(col1)
akrun
  • 874,273
  • 37
  • 540
  • 662