简体   繁体   中英

R - Number Unique Vals in Col1 of a Data Table when Col2 > Col3

SampleTable:

ID      Score1      Score2
1       100         88
1       96          94
1       94          95
2       100         100
2       98          94
3       77          88

So I want the return value to be 2, since there are 2 unique people who have an instance where Score1 > Score2.

For reproducability :

df = data.frame( ID=c(1,1,1,2,2,3), Score1=c(100,96,94,100,98,77), Score2=c(88,94,95,100,94,88) )
ID Score1 S

I was thinking

length( unique( which( df$Score1 > df$Score2 ) ) )

However that returns 3, clearly because it doesn't account for looking for the df$ID that is unique, just the number of unique occurences. How do I account for wanting the unique number of unique df$ID ?

I think you're looking for this in base R:

length(unique(df$ID[df$Score1 > df$Score2]))
[1] 2

Or using data.table :

library(data.table)
setDT(df)[Score1 > Score2, uniqueN(ID)]

Or dplyr :

library(dplyr)
df %>% filter(Score1 > Score2) %>% { n_distinct(.$ID) }

建立您的代码,获得unique的ID

length(unique(df[df$Score1>df$Score2,1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM