Aggregating data with 2 columns in data.table (r language) -


i have table(input):

date         directorname      companyname  rank 2015-08-01   sergey            vino          29 2015-08-02   sergey            vino          42 2015-08-09   sergey            vino          25 2015-08-04   sergey            vino          27 2015-08-05   mike              bolder        29 2015-08-01   mike              bolder        27 2015-08-11   mike              bolder        23 2015-08-09   mike              bolder        30 2015-08-09   jay               bolder        2 2015-08-10   jay               bolder        10 2015-08-11   jay               bolder        31 

i want know directorname reached top 30 in rank companyname never reached top 10. output:

    date         directorname      companyname  rank     2015-08-01   sergey            vino          29     2015-08-02   sergey            vino          42     2015-08-09   sergey            vino          25     2015-08-04   sergey            vino          27 

thanks help!

we can try in two-step process using data.table. convert 'data.frame' 'data.table' (setdt(df1)), grouped 'companyname' if all 'rank' greater 10 subset data.table (.sd) (in current example gives expected output). in second step, group 'directorname', check if any elements of 'rank' greater 30, subset again.

library(data.table) setdt(df1)[, if(all(rank > 10)) .sd, companyname][, if(any(rank >30)) .sd , directorname]  #   companyname       date directorname rank #1:        vino 2015-08-01       sergey   29 #2:        vino 2015-08-02       sergey   42 #3:        vino 2015-08-09       sergey   25 #4:        vino 2015-08-04       sergey   27 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -