user13467695
user13467695

Reputation:

How to count number of unique values in column based on values in other column

I have a dataframe:

col1    col2
1        10
1        -2
2        -4
3        5
4        8
4        17

I want to count number of unique values in col1, if at least once on same row there was positive value in col2. So, here answer must be 3, cause unique value 1,2,3 comply with the requirement (1 because there was case when col2 had value 10). value 2 from col1 doesn't comply requirement. How to do that?

Upvotes: 0

Views: 1142

Answers (5)

ThomasIsCoding
ThomasIsCoding

Reputation: 101064

Try this base R option

nrow(
  unique(
    subset(
      df,
      col2 > 0,
      select = -col2
    )
  )
)

which gives

[1] 3

A data.table option

> setDT(df)[col2 > 0, uniqueN(col1)]
[1] 3

Upvotes: 1

Roman
Roman

Reputation: 17648

You can try

dplyr::n_distinct(df1$col1[df1$col2 > 0])

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

Here is a base R one-liner.

length(unique(df1$col1[df1$col2 > 0]))
#[1] 3

Data

df1 <- read.table(text = "
col1    col2
1        10
1        -2
2        -4
3        5
4        8
4        17
", header = TRUE)

Upvotes: 3

Ronak Shah
Ronak Shah

Reputation: 388817

You can use -

library(dplyr)

df %>% summarise(result = n_distinct(col1[col2 > 0]))

#  result
#1      3

Or in base R -

length(unique(df$col1[df$col2 > 0]))

Upvotes: 2

iago
iago

Reputation: 3256

Assuming your dataframe is called df and that you mean

So, here answer must be 3, cause unique value 1,2,4 comply with the requirement

library(dplyr)
df %>% filter(col2 > 0) %>% summarise(n = n_distinct(col1))

Upvotes: 2

Related Questions