Reputation:
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
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
Reputation: 76402
Here is a base R one-liner.
length(unique(df1$col1[df1$col2 > 0]))
#[1] 3
df1 <- read.table(text = "
col1 col2
1 10
1 -2
2 -4
3 5
4 8
4 17
", header = TRUE)
Upvotes: 3
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
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